Home > Powershell, Toolbox > Script of the day – ping a range of IP addresses

Script of the day – ping a range of IP addresses

Friday today, so just a quick and easy script . . nothing clever

In PowerShell, ping functionality can be handled by using the ‘test-connection’ cmdlet, or simply using .Net

A ping using test-connection is just:

$object = New-Object system.Net.NetworkInformation.Ping
$object.Send('127.0.0.1')

You can wrap it as function to get even more out of this:

function ping-ip {
param( $ip )
trap {$false; continue}
$timeout = 1000
$object = New-Object system.Net.NetworkInformation.Ping
(($object.Send($ip, $timeout)).Status -eq 'Success')
}
ping-ip 127.0.0.1
ping-ip "news.bbc.com"

You can use this function to ping IP addresses or hostnames and will get either TRUE or FALSE as a return . . so you can use this in an if statement or similar

e.g.

If (Ping-IP "labserver001.local"){write-host "Lab domain appears to be online"}

You can add a loop to create your own network segment scan:

0..255 | % { $ip = "192.168.2.$_"; "$ip = $(ping-ip $ip)" }
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.