Power Ping

This custom class for PowerShell 5 and better allows you to “ping” multiple machines very quickly and easily using a parallel processing technique.  The object you create with this class contains various results such as:

  • The full “Test-Connection” results
  • The list of online machines
  • The list of offline machines
  • The count and percentage of online and offline machines
  • The execution time of the command

The class uses the “Test-Connection” cmdlet (which uses a WMI Win32_PingStatus method by the way), so the full results returned by this cmdlet can be viewed.  .Net runspaces are used to create a parallel processing environment to ensure quick results.

captureDownload the Class

Download from the TechNet Gallery.  Run the code in your session to make the class available, or follow the instructions in my post on creating a class library to automatically import custom classes into your session.

Using the Class

The easiest way to use the class is to create a custom object of this type.  You must pass an array of computer names which you can import from a csv file, or simply type out an array of computers, for example:

[ping]$Ping = 'PC001','PC002','PC003'

Or…

[ping]$Ping = Get-Content C:\temp\computerstoping.csv

Or…

[ping]$Ping = $Computers

After you hit enter, the code will run and ping the machines in the list in parallel.

To return all the properties of the object, simply type the variable name as in the screenshot above:

$Ping

To access any individual property of the object, simply use the dot operator:

$Ping.ExecutionTime

To view the list of computers that are online and responded to the ping, view the “online” property:

$Ping.Online

To view the full results for those machines that responded to ping, view the “results” property:

$Ping.Results

To view the full list of properties returned for an individual machine, you can use the index number, or a where method for example.

$Ping.Results[1] | Select *
$Ping.Results.Where{$_.Address -eq "PC001"} | Select *

You can also create a new instance of the class type without storing it in memory as a variable, for example if you just wanted to see which computers were online from a list, you can do this:

[ping]::new($(Get-content C:\temp\computers.csv)).Online

The parallel processing uses a session limit of 64, but you can change this in the class code towards the end.

Advertisements

2 thoughts on “Power Ping

  1. Awesome class it simplifies things quite a bit,I created a similar script which was multi threaded but without a class, I added a retry if the host if down on the first attempt.

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