Why Doesn't My Ping Script Run on Windows 2000 Computers?
Hey, Scripting Guy! I have a script that pings a computer before it tries to connect to it. The script works fine on Windows XP, but doesn’t work at all on my Windows 2000 computers. Do you have any idea why?
Hey, AK. As a matter of fact, we do have an idea why. Here’s a simplified version of your script, which pings the computer with the IP address 192.168.1.1:
strComputer = "192.168.1.1" Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_PingStatus " & _ "Where Address = '" & strComputer & "'") For Each objItem in colItems If objItem.StatusCode = 0 Then WScript.Echo "Reply received." End If Next
Why does this work on Windows XP but not Windows 2000? Simple: the Win32_PingStatus class is supported on Windows XP and Windows Server 2003, but not on any other versions of Windows. If you run this script on an XP machine it works; that’s because XP knows what the Win32_PingStatus class is. If you run it on Windows 2000 it fails; that because Windows 2000 has no idea what you’re talking about when you reference a class named Win32_PingStatus. Why, Windows 2000 has never even heard of such a thing!
Fortunately, there’s a way to work around this. After all, you don’t need the Win32_PingStatus class to ping a computer; Ping.exe works just fine. In this revised script, we just run Ping.exe, capture the output, and then check to see if the word Reply is found anywhere within that output. If it is, we assume that a reply was received from the remote machine. That’s not a 100% foolproof method, but it’ll work most of the time:
Set objShell = CreateObject("WScript.Shell") Set objExecObject = objShell.Exec _ ("%comspec% /c ping -n 3 -w 1000 192.168.1.1") Do While Not objExecObject.StdOut.AtEndOfStream strText = objExecObject.StdOut.ReadAll() If Instr(strText, "Reply") > 0 Then Wscript.Echo "Reply received." Else Wscript.Echo "No reply received." End If Loop
This script uses the Windows Script Host Exec method, which was introduced with WSH 5.6. Why do you care about that? Well, that means, if you haven’t done so already, you’ll have to upgrade to WSH 5.6 to get this script to run on your Windows 2000 computers. For more information about downloading and installing WSH 5.6, click here. And if you’d like to know a little bit more about the Exec method, check out this portion of the Microsoft Windows 2000 Scripting Guide.
Of course, if for some reason you can’t install or don’t want to install WSH 5.6 (and we’ve actually encountered a few people in that position), well, even then there’s no reason to despair. Here’s a script that will work on WSH 2.0. It’s a bit crazy, to say the least: it pings a computer, saves the output to a temporary text file, opens the text file and looks for the word Reply, deletes the file, and then reports the results. But, crazy or not, it’ll still do the trick:
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objShell = CreateObject("Wscript.Shell") objName = objFSO.GetTempName objTempFile = objName objShell.Run "cmd /c ping -n 3 -w 1000 192.168.1.1 >" & _ objTempFile, 0, True Set objTextFile = objFSO.OpenTextFile(objTempFile, 1) Do While objTextFile.AtEndOfStream <> True strText = objTextFile.ReadLine If Instr(strText, "Reply") > 0 Then Wscript.Echo "Reply received." Exit Do End If Loop objTextFile.Close objFSO.DeleteFile(objTempFile)