Lewis Wheeler
Lewis Wheeler

Reputation: 220

Powershell error - GetHostByAddress

Stumped on a problem here - running Powershell 1.0

Code (Assume an ip address which is valid is being passed in):

$ips = @(Import-CSV $attachmentLocation)
foreach ($ip in $ips){
    $ipAddress = $ip.IPAddress
$length = $ipaddress.length
write-host "Length is: ($length)"
    if(Test-Connection -ComputerName $ipAddress -Count 1 -ea silentlycontinue) {
        write-host $ipAddress
        $hostData = ([Net.Dns]::GetHostByAddress($ipAddress)).HostName
    }
}

Output:

Length is: (11)
10.xx.xx.xx
Exception calling "GetHostByAddress" with "1" argument(s): "The requested name is valid, but no data of the requested type was found"
At FileName:13 char:43
+         $hostData = ([Net.Dns]::GetHostByAddress <<<< ($ipAddress)).HostName
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

If I run the following it works fine - seems to be data type issue (im passing in a string value):

$hostData = ([Net.Dns]::GetHostByAddress("10.xx.xx.xx")).HostName

Working Code:

$ipAddress = "10.xx.xx.xx"
$hostData = ([Net.Dns]::GetHostByAddress($ipAddress)).HostName

Answer: Issue was with the ActiveDirectory Domain DNS resolution not the command, while some IP addresses were pingable they were not resolving properly on machine where the script was run. This was causing the error 'no data of the requested type was found' refers to the fact it couldn't resolve the IP to a DNS name.

Upvotes: 0

Views: 10508

Answers (2)

GaryG
GaryG

Reputation: 13

Running PS3, I get the error shown when traversing subnets on our domain. Especially remote locations. I'm checking 5 different C-class subnets on our domain to make sure there aren't devices we don't have in AD. It's also possible some devices aren't PCs with a hostname: routers, switches, firewalls, scanners, and the like. When my code gets to my local office, no error.

I'm not using a file, instead I generate the subnets via code in the script.

Upvotes: 0

Frode F.
Frode F.

Reputation: 54881

I've got two ideas you could try:

  1. GetHostByAddress() supports string and ipaddress. So try casting to ipaddress-type before running the function.

    if(Test-Connection -ComputerName $ipAddress -Count 1 -ea silentlycontinue) { write-host $ipAddress $hostData = ([Net.Dns]::GetHostByAddress(([ipaddress]$ipAddress))).HostName }

  2. If you're on PS 1.0, then your first priority should be to upgrade the machines to at least PowerShell 2.0. Nothing works well in PS 1.0.

Upvotes: 2

Related Questions