Nslookup & Ping a list of names & IPs using PowerShell

Have you ever needed to perform a nslookup or ping on a long list of computers and wished you had a PowerShell script you could run to automate the job? I did, so here’s the simple script I came up with…

The very basic commands needed to make this happen are:

To get PowerShell to perform nslookup:

[system.net.dns]::resolve('servername')

To get PowerShell to do a ping:

test-Connection -ComputerName 'servername'

In my final script, I piped the nslookup command to Select just the “HostName” and “AddressList” since it looks nice, and allows me to see the DNS name if I give it a list of IP addresses, and I can see the IP address if I give it a list of DNS names.

In my final script below you’ll see that I also added a switch telling it to try each ping twice before deciding it’s not responding, and that it should remain quiet about errors, since I manually configured the output a line or two below that.

In both cases I replaced the ‘servername’ with a variable representing each item on the list of IPs or servers, which I’m calling “$address”.

So, here’s the final script:

### This script performs nslookup and ping on all DNS names or IP addresses you list in the text file referenced in $InputFile.
### (One per line.) (Names or IPs can be used!) Outputs to the screen - just copy & paste the screen into Excel to work with results.

$InputFile = 'C:\Temp\list.txt'
$addresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile
    while($reader.ReadLine() -ne $null){ $TotalIPs++ }
write-host    ""    
write-Host "Performing nslookup on each address..."    
        foreach($address in $addresses) {
            ## Progress bar
            $i++
            $percentdone = (($i / $TotalIPs) * 100)
            $percentdonerounded = "{0:N0}" -f $percentdone
            Write-Progress -Activity "Performing nslookups" -CurrentOperation "Working on IP: $address (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone
            ## End progress bar
            try {
                [system.net.dns]::resolve($address) | Select HostName,AddressList
                }
                catch {
                    Write-host "$address was not found. $_" -ForegroundColor Green
                }
            }
write-host    ""            
write-Host "Pinging each address..."
        foreach($address in $addresses) {
            ## Progress bar
            $j++
            $percentdone2 = (($j / $TotalIPs) * 100)
            $percentdonerounded2 = "{0:N0}" -f $percentdone2
            Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
            ## End progress bar
                if (test-Connection -ComputerName $address -Count 2 -Quiet ) {  
                    write-Host "$address responded" -ForegroundColor Green 
                    } else 
                    { Write-Warning "$address does not respond to pings"              
                    }  
        }
write-host    ""        
write-host "Done!"

As you can see, you can give it a list of IP addresses and/or DNS names, which it reads in from a text file in C:\Temp\list.txt. (One item per line.)  It calls that list “$addresses” and then performs a nslookup & then a ping on each address in the list of addresses, and outputs the results to the screen.

If the machine doesn’t exist it, will tell you that, and if it doesn’t respond to pings it will tell you that too (in a different font color, so it’s easier to see).

It also displays a progress bar across the top as the script is running to give you a sense of how much longer it’ll take. It does one progress bar for nslookups, and then another bar when it does the pings.

Obviously, you don’t need to run both pings and nslookups if you don’t want to – just comment out the section you don’t need.

I’m no PowerShell guru, so there may be a better way to do some of this, but I recently ran it on a few different lists of over 600 servers each, and it worked great, and didn’t take all that long (relatively speaking).

If you want to use this in Excel, just copy the screen output and paste it into Excel. For nslookups output you can then use the “Text to Columns” button in the “Data” tab, and tell it it’s space delimited and to “treat consecutive delimiters as one.” That will help separate things into easier to work with columns. You can then do a find/replace on the curly brackets to get rid of them. (Or you could just modify the PowerShell commands to output properly to CSV, and then add the lines necessary to do that, but I was in a hurry and didn’t feel like it, since I’m faster in Excel. ;)

Now you’ll never have to wonder which machines or IPs are still on your network. :)

Steve