PowerShell for Windows Admins

Aug 22 2011   1:03PM GMT

Ping a subnet

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I recently needed to find which IP addresses were active on a subnet to track down a printer. Quickest way was to ping them.

 

function ping-subnet {            
param (            
 [parameter(Mandatory=$true)]            
 [ValidatePattern("\b\d{1,3}\.\d{1,3}\.\d{1,3}\b")]            
 [string]$subnet            
)            
            
0..255 | foreach {             
             
 $address = "$subnet.$_"            
            
 if (Test-Connection -ComputerName $address -Count 1 -Quiet){            
    Test-Connection -ComputerName $address -Count 1            
 }            
            
}            
}

 

Put the subnet in as a parameter. Then work through 0 – 255 building an address from the subnet. use test-connection in quiet mode and if it responds do a 1 count test-connection (ping) to get the result.

Not necessarily the quickest but it was quick to throw together and it works

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: