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.
Download the Class
Download from GitHub. 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.csvOr…
[ping]$Ping = $ComputersAfter 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:
$PingTo access any individual property of the object, simply use the dot operator:
$Ping.ExecutionTimeTo view the list of computers that are online and responded to the ping, view the “online” property:
$Ping.OnlineTo view the full results for those machines that responded to ping, view the “results” property:
$Ping.ResultsTo 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)).OnlineThe parallel processing uses a session limit of 64, but you can change this in the class code towards the end.
Loved it
Great class!
Technet is gone. Could you post the class to your Github?
Sorry, I updated the post with the GitHub link now.