I recently had the need to reconcile IP Addresses against HostNames to ensure that my local DNS server had an accurate match. Since I needed to do this quickly, I used invoke-expression to call NsLookup.exe but you can undoubtedly achieve the same result directly with .Net methods.
To do this in a script, we need to do some parsing of NsLookup’s multiline output. Also, for my situation, the original hostname is not fully-qualified, so (as you can see below) I strip off any domain name suffix from the reverse lookup results.
Here’s the code:
$IpAddress = "192.168.1.1"
$Hostname = "myHostname"
$revLookupName = iex "nslookup $IpAddress" |? {$_ -like "name*"} |% {$_.split(":")[1].trim()}
write-host "NameMatch for $IpAddress : " -nonewline -fore darkcyan
write-host "$($n.split(".")[0].tolower()) = " -nonewline
write-host "$($Hostname.tolower()) ? " -nonewline
if ($revLookupName.split(".")[0].tolower() -eq ($Hostname.tolower()) {
write-host "OK" -fore green
}
else {
write-host "NOMATCH" -fore red
} |
useful piece of code, right what I needed. thanks.