class Inspec::Resources::WindowsHostProvider

def ping(hostname, port = nil, _proto = nil)

def ping(hostname, port = nil, _proto = nil)
  # ICMP: Test-NetConnection www.microsoft.com
  # TCP and port: Test-NetConnection -ComputerName www.microsoft.com -RemotePort 80
  request = "Test-NetConnection -ComputerName #{hostname} -WarningAction SilentlyContinue"
  request += " -RemotePort #{port}" unless port.nil?
  request += "| Select-Object -Property ComputerName, TcpTestSucceeded, PingSucceeded | ConvertTo-Json"
  cmd = inspec.command(request)
  begin
    ping = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return {}
  end
  { success: port.nil? ? ping["PingSucceeded"] : ping["TcpTestSucceeded"] }
end

def resolve(hostname)

def resolve(hostname)
  addresses = []
  # -Type A is the DNS query for IPv4 server Address.
  cmd = inspec.command("Resolve-DnsName –Type A #{hostname} | ConvertTo-Json")
  begin
    resolve_ipv4 = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end
  resolve_ipv4 = resolve_ipv4.inject(:merge) if resolve_ipv4.is_a?(Array)
  # Append the ipv4 addresses
  resolve_ipv4.each_value do |ip|
    matched = ip.to_s.chomp.match(Resolv::IPv4::Regex)
    next if matched.nil? || addresses.include?(matched.to_s)
    addresses << matched.to_s
  end
  # -Type AAAA is the DNS query for IPv6 server Address.
  cmd = inspec.command("Resolve-DnsName –Type AAAA #{hostname} | ConvertTo-Json")
  begin
    resolve_ipv6 = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end
  resolve_ipv6 = resolve_ipv6.inject(:merge) if resolve_ipv6.is_a?(Array)
  # Append the ipv6 addresses
  resolve_ipv6.each_value do |ip|
    matched = ip.to_s.chomp.match(Resolv::IPv6::Regex)
    next if matched.nil? || addresses.include?(matched.to_s)
    addresses << matched.to_s
  end
  addresses
end