Simple Powershell Ping Sweep

Below is code for a very simple powershell ping sweep.  I used this code to automate a process that needed to know which hosts were alive on a subnet.

 
$ping = New-Object System.Net.NetworkInformation.Ping

$IPScope = "192.168.1";

$IPNode = 1;

while ( $IPNode -le 255 ) {

 $IPtoPing = "$IPScope.$IPNode";
 $stat = $ping.send($IPtoPing) | select status;
 $result = "$IPtoPing $stat";
 $result;
 $IPNode = $IPNode + 1;

}
Advertisements

2 responses to “Simple Powershell Ping Sweep

  • Boe Prox

    If you are using PowerShell V2, you can use Test-Connection with the -Count and -Quiet parameters instead to perform the network connectivity check.

    1..255 | ForEach {If (Test-Connection 192.168.1.$_ -Count 1 -Quiet) {“192.168.1.{0}: Online” -f $_} Else {“192.168.1.{0}: Offline” -f $_}}

    • razor3dg3

      I really like the Test-Connection command, thank you. I went ahead and threw together a quick script using your code above. I really like the result.

      $IPNode = 1;

      while ( $IPNode -le 255 )
      {
      If (Test-Connection 192.168.1.$IPNode -Count 1 -Quiet) {
      “192.168.1.{0}: Online” -f $IPNode;
      } Else {
      “192.168.1.{0}: Offline” -f $IPNode;
      }

      $IPNode = $IPNode + 1;
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: