14 April 2017

Pinging a subnet range using PowerShell

Every once in a while I come across a network with sub-optimal documentation. I usually want to add a new device to the network without having to hunt for a free IP address. One of the simple tests to see if an IP address is in use, is sending a ping. You can use a network scanner to ping an entire IP subnet or you can script something yourself. This is my PowerShell based script that I use in these cases:

# Ping an IP range
# based on PoshPortScanner.ps1 (https://blogs.technet.microsoft.com/heyscriptingguy/2014/03/19/creating-a-port-scanner-with-windows-powershell/)
$Net = "192"
$Brange = "168"
$Crange = 2..8
$Drange = 1..254
$Logfile = C:\users\Pietje\Desktop\ping-output.txt
foreach ($B in $Brange) {
foreach ($C in $Crange) {
foreach ($D in $Drange) {
 $ip = {0}.{1}.{2}.{3} -F $Net,$B,$C,$D
 if(Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip)
   { $ip, responding to ping >> $Logfile}

No comments:

Post a Comment