Ezine 151 – Compare PowerShell with the Windows GUI

Scripting with PowerShell’s – Win32_PingStatus

There are occasions when it’s useful to know whether a computer is online, or whether it’s unavailable.  Ping is the classic command to test the status of a ComputerName or IP address.  WMI (Windows Management Instrumentation) provides a class called Win32_PingStatus, which we are going to put to work with PowerShell’s get-WmiObject.

Win32_PingStatus  Topics

Real Life Tasks

The rationale behind Win32_PingStatus is that it can give you a handle on whether or not a machine is available on the network.  If a particular machine is running then the rest of our PowerShell script can ‘do stuff’.  That ‘stuff’ could be saving files, or opening eventlogs.  By using a construction such as: if StatusCode = zero, then go ahead, else try another machine; you can avoid your script failing just because the target machine is offline.

Most real life tasks, which PowerShell solves, are multi-dimensional.  In this instance we have to collect TCP/IP information such as ping status values with Win32_PingStatus, then master PowerShell constructions, such as Do…Until loop, and also remember the syntax for get-WmiObject.  As usual the secret of success is to break the task in to manageable chunks.  The bonus of this approach is that you can understand what is going on, and thus adjust my example scripts to suit your circumstances.

Example 1 – List the WMI Classes

The idea behind this basic script is merely to research possible WMI classes, and to gain experience with the vital command: get-WmiObject.  My hidden agenda is to explain how I knew there was a class called Win32_PingStatus.

# PowerShell script to list the Win32 classes
get-WmiObject -List | where {$_.name -match "Ping"}

Note 1:  The benefit of using -match rather than -like or -eq is that we only need a partial match.  It is also easy to  modify the code to search for other Win32 classes.  For example amend to: get-WmiObject -List | where {$_.name -match "Network"}

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 2 – Win32_PingStatus

Compared with other Win32 classes, Win32_PingStatus is unresponsive to help and to get-member.  My breakthrough came when I discovered the -f, or -filter parameter; for example -filter "Address=’IP’ " or even -filter "Address=’IP’ ".

# PowerShell script to test the status of an IP address
get-WmiObject Win32_PingStatus -filter "Address=’192.168.1.150′ "

Note 1:  The key construction is the "Address=".  Pay close attention to the speech marks.  Double speech marks around the "whole address", single speech marks around the ‘IP’ portion.  Remember to introduce "Address" with -f or the full word, -filter.

Note 2: Many people prefer the alias gwmi instead of get-WmiObject.

Note 3:  In the resulting output concentrates on StatusCode.  For once, zero is good news, it means that Ping has been successful and found the IP address.  On the other hand, a binary value of 11010 means that ping failed.

Challenge: Substitute a hostname for the IP address.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 3 – What to do with StatusCode

In example 3 we get down to business, and test a whole range of IP addresses.  For this we need a PowerShell loop.  There are dozens of ways of achieving this, I happen to have chosen Do…Until.  I would like to emphasise that my goal was to test the following range of IP addresses 192.168.1.1 to 192.168.1.20.   If you don’t use this IP range then please amend my line: $IpAdr = "192.168.1." to the network address of your local subnet.  The safest, but most tedious way of checking all the IP address is to increase $i -eq 20 to $i -eq 254.

$i =1
$IpAdr = "192.168.1."
Do { $Ip4th = $IpAdr + $i
$Pingy = get-WmiObject Win32_PingStatus -f "Address=’$Ip4th’"
$Pingy | Format-Table Address, StatusCode -hideTableHeaders -auto; $i++
}
until ($i -eq 20)

Results: A StatusCode of 0 means the machine responded to ping.  Other values such as 1101 means that ping could not find that particular IP address.

Example 4 – PowerShell script to test which IP addresses are online

The most important feature of example 4 is to recognise where the script could deliver a payload, or ‘do stuff’.  If you want a subroutine which wrote to a share on the machine, or extracted it’s eventlogs then replace the contents of the {brackets} after the ‘if’ and ‘else’ commands with your ‘stuff’.

$i =1
$Ip = "192.168.1."
Write-Host "IP Address"
Write-Host "—————————————-"
Do { $Ip4th = $Ip + $i
$Pingy = get-WmiObject Win32_PingStatus -f "Address=’$Ip4th’"
if($Pingy.StatusCode -eq 0) {
     "{0,0} {1,5} {2,5}" -f
     $Pingy.Address, $Pingy.StatusCode," ON NETWORK"}
     else
     {"{0,0} {1,5} {2,5}" -f $Pingy.Address, $Pingy.StatusCode, " xxxxxxxxx"
     }
$i++
}
until ($i -eq 20)

Note 1: Much of this script is cosmetic, and represents my thought processes in creating a script which detects which IP addresses are ‘ON NETWORK’.  You could write much tighter code.

Note 2: "{0,0} {1,5} {2,5}" -f is to format, or to space the output.  The first number is the item of each pair (0,1,2), the second is the tabbing or padding (0,5,5).  Feel free to adjust the second number in each pair.

Note 3: One of the most important parts of the script is the ‘if’ statement:
if($Pingy.StatusCode -eq 0).  What this does is to test if we have any StatusCode responses of zero, if so we assume that that IP Address in online.

Summary of Win32_PingStatus

Before your script delivers a payload at a remote machine on the network, it’s useful to check that the status of the machine is available.  The feature of these examples is Win32_PingStatus, and the way it checks the StatusCode value.  From a pure PowerShell point of view this script also demonstrates the Do…Until loop and the -f formatting technique.  My advice is to use this script as a template, to which you add a payload that ‘does stuff’ at the remote machine.

  See more about Win32_PingStatus and other PowerShell scripts here

If you like this page then please share it with your friends

 


See more Microsoft PowerShell v 3.0

PowerShell 3.0  • What’s New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where