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.
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 are multi-dimensional. In this instance
PowerShell collects TCP/IP information such as ping status values with Win32_PingStatus.
The next stage is to develop PowerShell constructions, such as Do...Until loop, and
also
remember the syntax for the parent cmdlet Get-WmiObject.
As usual the secret of success is to break down 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.
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"}
Calculating IP Address ranges is a black art, which many network managers
solve by creating custom Excel spreadsheets. IPAT cracks this problem of
allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, just enter the network
address and the subnet mask, then IPAT works out the usable addresses and their
ranges. For Mr Lazy IPAT discovers, and then displays, the IP addresses of existing
computers.
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 Win32_PingStatus to test the status of an IP address $AddrIP
= "Address='192.168.1.10' " Get-WmiObject Win32_PingStatus -filter
$AddrIP
Note 0: While not strictly necessary, I introduced the
variable $AddrIp to highlight the need for you modify this IP address to
suit your network.
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.
In example 3 we get down to business, and ping 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.
Results: A StatusCode of 0 means the machine responded to ping. Other values such as 1101,
or 11003 mean that ping could not find that particular IP address.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
Solarwinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMWare
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer.
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'.
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.
Thus utility makes it easy to check the health of a router or
firewall. Check the real-time performance, and availability statistics, for any device
on your network. Get started with an extensive collection of "out-of-the-box"
monitors for popular network devices.
With a PowerShell ping computer script you can check the status of the
machine before your main script delivers its payload. 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.
If you like this page then please share it with your friends
Please email me if you have a script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.