Ping Multiple Hosts with PowerShell and Return Useful Information

Hi

I want to ping multiple hosts from the powershell/CMD and needs to get information like host is UP or Down and host's IP adress.

In case host is down it should still return the IP address of the host.

If host doesnt exist then no worries.

I tried below command but its not returning IP ADDRESS for hosts which are down but EXIST.  

$Host = Test-Connection $server -Count 1 -Quiet -ErrorAction SilentlyContinue $ip = Test-Connection $server -Count 1 | select ipv4address -ErrorAction SilentlyContinue $ip = $ip.IPV4Address   if ($Host.ToString() -like "true") {       Write-Host "$server $ip is pingable" -ForegroundColor green     Write-Output "$server,$ip,yes" | Out-File $OutFile -Append   } else {     Write-Host "$server not pingable" -ForegroundColor Red     Write-Output "$server,$ip,no" | Out-File $OutFile -Append   }
January 22nd, 2015 10:54am

Use this:

[system.net.dns]::GetHostAddresses($server).IPAddressToString

Free Windows Admin Tool Kit Click here and download it now
January 22nd, 2015 11:31am

This works charm, however when host name is does not exist it gives below error on power shell screen, can we suppress it ?

i tried this but doesnt work. ($ip = [System.Net.Dns]::GetHostAddresses($Machine) | select IPAddressToString -ExpandProperty IPAddressToString -ErrorAction SilentlyContinue)

Exception calling "GetHostAddresses" with "1" argument(s): "No such host is known"
At line:5 char:1
+ $ip = [System.Net.Dns]::GetHostAddresses($Machine) | select IPAddressToString -E ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SocketException

January 22nd, 2015 2:34pm

This works charm, however when host name is does not exist it gives below error on power shell screen, can we suppress it ?

Yes:

http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx

https://technet.microsoft.com/en-us/library/hh847793.aspx

Free Windows Admin Tool Kit Click here and download it now
January 22nd, 2015 5:05pm

Check out this post in my blog: http://gozettabyte.com/tech-blogs/ping-multiple-hosts-with-powershell-and-return-useful-information/ Should be what you're looking for or close.
February 1st, 2015 10:46pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics