Scripting Files with PowerShell's - Win32_PingStatusScripting with PowerShell's - Win32_PingStatusThere 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 TasksThe 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 ClassesThe 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 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_PingStatusCompared 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 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 StatusCodeIn 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 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 LANSurveyor
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |