0

I want to write script which returns hostnames with no IP. I wrote following part of the script but I don't know how can I return the empty answers. Thanks in advance

$numbers=1..255
foreach ($number in $numbers){
{
    nslookup host$number
}
  • is there a reason NOT to use the powershell cmdlet for this? look up `powershell nslookup` for info on that. – Lee_Dailey Jan 11 '22 at 20:46

1 Answers1

1

If you want to use nslookup from Powershell, you will have to process the returned result as it is just text. Instead, use Resolve-DnsName Powershell cmdLet.

Note that the command will return all DNS records, so the result can be an array of objects.

Here is a small example on how you can use it (I replaced your foreach loop with for):

for($i=1 ; $i -le 255 ; $i++) {

    $DnsQuery = $null
    [array]$DnsQuery = Resolve-DnsName -Name "host$i" -ErrorAction SilentlyContinue

    # If no DNS record found, process next host
    if($DnsQuery -eq $null) {
        Write-Host "No DNS record found for host$i"
        continue ;
    }

    # Look for DNS record without IP address
    foreach($Result in $DnsQuery) {
        if($Result.IPAddress -eq $null) {
            Write-Host "DNS record without IP address found for host$i (Record type: $($Result.Type))"
        }
    }

}
ZivkoK
  • 186
  • 2