How to get an IP address from a server list script

Here’s how you can get the list of IP addresses, FQDN and Ping status with a simple Powershell server list script.

For a start create a servers.txt file in C:\temp folder with the list of servers you would like to resolve. You need to put one server per line. You also need to have Microsoft Excel installed on the system since the result will be exported to .csv file.

C:\temp\servers.txt

servers.txt example:

hostname
hostname2
hostname3

Now create a new text file and rename it to serverlist.ps1

$counter = 1
$comps = get-content C:\TEMP\servers.txt
$dnsResults = "C:\TEMP\IP address.csv"

function get-dnsres{
foreach ($comp in $comps) {
$TempIP = ([system.net.dns]::GetHostAddresses($comp)) | select IPAddressToString

$status = "Processing system {0} of {1}: {2}" -f $counter,$comps.Count,$comp
Write-Progress 'Resolving DNS' $status -PercentComplete ($counter/$comps.count * 100)
$counter++
$comp |
select @{Name='ComputerName';Expression={$comp}}, `
@{Name='ResolvesToIP';Expression={[system.net.dns]::GetHostAddresses($comp)}}, `
@{Name='IPResolvesTo';Expression={([system.net.dns]::GetHostEntry($TempIP.IPAddressToString)).HostName}}, `
@{Name='PingStatus'; Expression={ `
if ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 0) {'Host Online'} `
elseif ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 11003) {'Destination Host Unreachable'} `
elseif ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq 11010) {'Request Timed Out'} `
elseif ((get-wmiobject -query "SELECT * FROM Win32_PingStatus WHERE Address='$comp'").statuscode -eq $Null) {'NoDNS'}
}
}
}
}

get-dnsres | export-csv $dnsResults -notypeinformation
invoke-item $dnsResults

Save the script to the same folder at C:\temp.

Before you’ll be able to run the script you will have to enable execution policy on your system:

Start the Powershell in elevated mode (Run as Administrator) and run the following command:

Set-ExecutionPolicy RemoteSigned

Now run the Powershell script by using this command:

powershell -noexit “& “”C:\temp\serverlist.ps1″””

You should get the results on screen and also saved to C:\temp\IP address.csv file.

server list script

Please feel free to comment if you have any advices regarding this simple server list script.

There are also some third-party free tools available for download like:

NirSoft FastResolver
IP Tools

You may also like...

1 Response

  1. Sonny says:

    This was very helpful. Thank you.
    Just wondering, with the server list text file, is there a way to list IP addresses with their host name and run it in powershell?

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve − eleven =

This site uses Akismet to reduce spam. Learn how your comment data is processed.