Downloads
46120.zip

Sometimes, administrative scripts need to connect to more than one computer. For example, sometimes my administrative scripts use Active Directory Service Interfaces (ADSI) or Windows Management Instrumentation (WMI) to get information from multiple remote machines. These scripts can take a long time to run when all the computers aren't powered on and connected to the network. In these scripts, I find it useful to ping remote computers before attempting to connect to them.

Windows provides a ping utility called ping.exe, but its exit code isn't documented. Some versions of ping.exe always return an exit code of 0, even when the ping is successful. This exit code inconsistency makes it difficult to determine a response from a script. To solve this problem, I created a VBScript function called Alive. The Alive function uses ping.exe to ping the specified host, but unlike ping.exe, the function always returns a value of True when the host responds to the ping. For .cmd scripts, I wrote IsAlive.cmd. This snippet always returns an exit code of 0 when the ping succeeds.

The Alive Function for .vbs Scripts
As Listing 1 shows, the Alive function works by first generating a temporary filename in the Temp folder. It uses a VBScript Do...Loop statement to ensure that the file doesn't already exist. Next, the function executes ping.exe and uses cmd.exe to redirect ping.exe's output to the temporary file. It then reads the contents of the temporary file into a TextStream object, which is part of the Microsoft Scripting Runtime Library. The function uses cmd.exe and output redirection rather than Windows Script Host's (WSH's) WshScriptExec object because there's no way to hide the console window when you use the WshScriptExec object from a script that's being executed by wscript.exe.

To check for the ping reply, the Alive function creates a VBScript RegExp object and sets that object's Pattern property to a regular expression that will match a ping reply. The function's result is set to the result of the RegExp object's Test method, which returns True when the reply pattern is found in the temporary file. The Alive function then closes and deletes the temporary file.

To use the Alive function, you simply add the code that Listing 1 shows to your script, then call the function using the syntax

Alive(Computer)

where Computer is the IP address or name of the computer you want to ping. For an example of how you might incorporate the Alive function in a script, see the article "Are the OS Service Packs on Your Remote Computers Up-to-Date?" May 2005, InstantDoc ID 45818.

IsAlive.cmd for .cmd Scripts
Not surprisingly, IsAlive.cmd works differently than the Alive function. As Listing 2 shows, IsAlive.cmd begins by checking for one required command-line argument that represents a host name. The snippet then attempts to ping the specified host. It pipes this output to findstr.exe, which uses a regular expression to look for a ping reply. The script exits with an exit code equal to Findstr's, which, although undocumented, is 0 when the ping succeeds and nonzero when the ping fails. Note that because Findstr's exit code is undocumented, it's important to test IsAlive.cmd to make sure it behaves reliably before using it in production scripts.

You can use the Call command to call IsAlive.cmd from other scripts, then use the If Errorlevel command or the ERRORLEVEL variable to check IsAlive.cmd's exit code. For example, this code might look like

Call Alive Computer
If %ERRORLEVEL% EQU 0 Goto :ALIVE

An Important Note
In Windows XP Service Pack 2 (SP2), the Windows Firewall is enabled by default and doesn't respond to pings. (It also doesn't respond to WMI connections.) So, you need to either disable the firewall or configure it to allow ping requests. For information about how to manually configure the Windows Firewall in XP SP2, go to http://www.microsoft.com/technet/community/columns/cableguy/cg0204.mspx or http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/connecting_through_windows_firewall.asp. For information about how to use Group Policy to configure the Windows Firewall, check out http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/mangxpsp2/mngwfw.mspx.