Home HowTo WMI Network Pinging With WMI
 

Pinging With WMI

WMI can easily ping remote systems for you with Win32_PingStatus. You do need to know a little trick, though.

Pinging IP Addresses

At first glance, the Win32_PingStatus class seems to be broken, because when you try and retrieve instances, you get back an error message:

PS> Get-WmiObject -Class Win32_PingStatus
Get-WmiObject : Not supported 
At line:1 char:1
+ Get-WmiObject -Class Win32_PingStatus
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementExceptio 
   n
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.G 
   etWmiObjectCommand

Actually, the class accepts input parameters that you first need to set before you can get back results. The most important input parameter is Address. It specifies the IP address or host name you want to ping:

PS> Get-WmiObject -Class Win32_PingStatus -Filter 'Address="127.0.0.1"'

Source        Destination     IPV4Address      IPV6Address                             
------        -----------     -----------      -----------                             
TOBI2         127.0.0.1       192.168.2.102    fe80::e872:bf1e:658c:494c%3

How Input Parameters Are Specified

The way Win32_PingStatus receives its input parameters is somewhat unusual: it takes them from your query. In the previous example, by filtering for Address, your filter criteria were submitted as input parameter.

You now know already how you can easily ping systems. This works for IP addresses as well as host names:

Get-WmiObject -Class Win32_PingStatus -Filter 'Address="127.0.0.1"'
Get-WmiObject -Class Win32_PingStatus -Filter 'Address="storage1"'

Pinging Multiple Systems

You can even ping multiple systems in one call, simply by adding more addresses to your filter query. This line would ping two machines:

PS> Get-WmiObject -Class Win32_PingStatus -Filter 'Address="powertheshell.com" or Address="powershellmagazine.com"'


Source        Destination     IPV4Address      IPV6Address                             
------        -----------     -----------      -----------                             
TOBI2         powertheshel... 108.162.206.76                                           
TOBI2         powershellma... 206.217.196.220

Since it is not especially convenient to construct a huge query string manually, here is a more elegant solution that allows you to add as many machines or IP addresses as you like:

$hosts = 'powertheshell.com', 'powershellmagazine.com', 'powershell.com'

$filter = 'Address="' + ($hosts -join '" or Address="') + '"'

Get-WmiObject -Class Win32_PingStatus -Filter $filter |
  Select-Object -Property Address, ProtocolAddress, ResponseTime

The result would look similar to the following output. Note that you can use Select-Object to select only some (or completely different) properties from the result:

Address                ProtocolAddress ResponseTime
-------                --------------- ------------
powertheshell.com      108.162.206.76            31
powershellmagazine.com 206.217.196.220          118
powershell.com         65.38.114.170            188

Using More Input Parameters

Win32_PingStatus has many more input parameters. The most frequently used was Address, but Timeout is also useful. It limits the ping operation to a certain timeout (specified in milliseconds).

So this call would take at most 300 milliseconds. If the site in question did not answer within this time, it would treated as non-responding (offline):

Get-WmiObject -Class Win32_PingStatus -Filter 'Address="powertheshell.com" and timeout=300'

These few examples are designed to get you curious and started. We’d love to hear from you, so if you create more samples or find out additional things, please take a minute to share with us. Add your findings as comment (comment form below is open to anyone). Thank you!

In addition, you will find the complete documentation here: http://www.powertheshell.com/reference/wmireference/root/cimv2/win32_pingstatus/. Our WMI reference, too, is a community-powered project, so you can leave comments there, too.