Windows PowerShell


Scripting Files with PowerShell's - Win32_PingStatus

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"}

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.

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.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

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 Windows PowerShell real life tasks

PowerShell Home  • Real life tasks  • IpConfig  • Exchange  • Com objects  • Services  • Syntax

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.