Search code examples
.netdnspowershell-2.0hostname

GetHostByAddress valid anymore?


All of my PS remoting tools are dependent on a function I wrote that runs the the following code to resolve the host name.

This stopped working today and now results in this error. I looked around and tried GetHostEntry, but this seems to only return the IP address.

$Computername = ([Net.Dns]::GetHostByAddress($IPAddress)).HostName

"Exception calling "GetHostByAddress" with "1" argument(s): "The requested name is valid, but no data of the requested type was found" At line:1 char:28 + [Net.Dns]::GetHostByAddress <<<< ($IP).HostName + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException"


Solution

  • First thing I would do is to independently confirm that the PTR records of interest are still in DNS. On Windows, I would use nslookup. For example, to find the host information for IP 10.1.2.3:

    nslookup
    > set type=ptr
    > 3.2.1.10.in-addr.arpa.
    Server:     127.0.0.1
    Address:    127.0.0.1#53
    
    ** server can't find 1.2.3.10.in-addr.arpa: NXDOMAIN
    >
    

    If you get back an NXDOMAIN, like the above, then doesn't matter what call you're using...the data just isn't there.

    If you do get back an answer, then maybe it is in fact the API call. This page https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostbyaddress?view=netframework-4.8 does specify that the GetHostByAddress method is obsolete and to use GetHostEntry, like you tried. The fact that GetHostEntry returned just the IP Address suggests that the PTR record is gone, or maybe inaccessible from where you are querying, or maybe you don't have DNSPermission as required by that call.