An Alternative Approach for Pinging Computers

There are many ways to check a computer for connectivity in a script. Here’s a function that pings computers by using WMI’s Win32_PingStatus object.

Readers

October 9, 2005

2 Min Read
ITPro Today logo in a gray background | ITPro Today


In "Snippets to Go: A Worry-Free Way to Ping Remote Computers," June 2005, InstantDoc ID 46120, Bill Stewart presents the Alive function, which uses ping.exe to ping computers in VBScript scripts. I wrote a VBScript function named Ping that performs the same task but uses a different approach. In the Ping function, I use Windows Management Instrumentation's (WMI's) Win32_PingStatus object rather than ping.exe. The Win32_PingStatus object, which is available in Windows Server 2003 and Windows XP, determines whether a host is reachable by using an Internet Control Message Protocol (ICMP) echo.

Listing 1 shows the Ping function. This function takes one parameter: a string value that specifies the name of the host to ping. The function returns the Boolean value of True when the host is reachable and False when it's not.

The Ping function begins by defining and initializing the objPing and objStatus variables. These variables will represent the Win32_PingStatus object and its StatusCode object, respectively. Next, the function creates an instance of the Win32_PingStatus object by executing a WMI query, as callout A in Listing 1 shows.

At this point, Win32_PingStatus sends a ping. The ping returns a collection object (i.e., the StatusCode object) that contains all the ping responses. The function assigns this collection to the objStatus variable. Using a For Each...Next statement, the function enumerates through the items in the objStatus variable. Because Win32_PingStatus can send only one ping (the number of pings isn't configurable in the current version of Win32_PingStatus), objStatus will contain only one item.

As callout B shows, the Ping function uses an If...Then...Else statement to check the item's status code. When the status code is 0, the function returns a True value. When the status code is any other value or Null, the function returns a False value. (You can find more information about the status codes and the Win32_PingStatus object at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp.)

As I mentioned earlier, the Ping function's only parameter is the name of the host to ping. Thus, to use the Ping function, you add the code that Listing 1 shows to your script, then call the function using the syntax

Ping(Host)

where Host is the name of the host you want to ping. To specify the host, you can use the machine's host name, Fully Qualified Domain Name (FQDN), or IP address.

Note that the Ping function won't work on machines running Windows 2000 or earlier because Microsoft introduced the Win32_PingStatus object in XP. In addition, as Stewart pointed out in "Snippets to Go: A Worry-Free Way to Ping Remote Computers," Windows Firewall is enabled by default in XP Service Pack 2 (SP2). Therefore, XP SP2 machines won't respond to pings (or WMI connection queries, although they're not required for this script) unless you disable Windows Firewall or configure it to allow ping requests. Stewart provides some helpful references to resources that can help you with this task.

—Chris Whitlow
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like