The VBScript Network and Systems Administrator's Cafe

Sep 4 2008   4:01PM GMT

Pinging a remote computer from another remote computer using the WMI Win32_PingStatus class in VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

I recently began working on trying to figure out ways to troubleshoot real problems with a VBScript and give me some diagnostic information about the current state of the environment. As I build this script I plan on sharing pieces of it bit by bit with you, my readers. I also wanted to get back to writing some WMI scripts that will help you all do your jobs more efficiently. Lastly, I wanted to begin building a “toolbox” script that you could use to write your own scripts. These are the goals I’m tracking toward in my next series of posts that contain VBScript code. Now onto this script…

The first piece of troubleshooting a problem, in my opinion, is ensuring that a communication path exists between two systems. Duh! However, simply pinging the systems individually from your desktop isn’t a good test… it only tests the your computer can communicate with the two systems, not that they can communicate with each other!

This script simply uses our friend WMI to make a call a remote computer requesting that computer ping another computer. Simple enough, but invaluable because how many times have you had to login to a remote computer to check if it can connect to a system? Now you don’t have to… you can do it from a script!

The script uses the Win32_PingStatus class in WMI. Essentially, it will only work on Windows XP and newer (Sorry, Windows 2000 and older doesn’t support the WMI class we need.) and the user executing the script will likely need to be an administrator on the system that is being called (but not necessarily being pinged). For further information on this class you can reference the Win32_PingStatus documentation.

Now lets get to the script!

 ‘Use a remote computer to ping another remote computer
Option Explicit
 
‘Change the SourceServer and RemoteServer Strings below to servernames or IP addresses for you.
wscript.Echo RemotePing(“SourceServer”, “RemoteServer”)
wscript.Echo “Done!”

Function RemotePing( SourceComputer, DestinationComputer)

 Dim strComputer1, strComputer2
 Dim objWMIService, colItems, objItem

 strComputer1 = SourceComputer
 strComputer2 = DestinationComputer

 On Error Resume Next
  ‘ error control block
  Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}//” & strComputer1 & “\root\cimv2″)
  Set colItems = objWMIService.ExecQuery (“Select * from Win32_PingStatus ” & “Where Address = ‘” & strComputer2 & “‘”)
  For Each objItem in colItems
      If objItem.StatusCode = 0 Then
          RemotePing = strComputer1& “: Reply received from ” & strComputer2 & ” in ” & objItem.ResponseTime & ” ms.”         
      Else
       RemotePing = “Error pinging ” & strComputer2 & ” from ” & strComputer1 & “. The status code returned was :” & objItem.StatusCode
      End If
  Next
 On Error GoTo 0
End function

As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files  from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Labnuke99
    Your URL is broken. Should it be http://www.websystemsadministration.com/ ?
    32,960 pointsBadges:
    report
  • Jerry Lees
    Oh my! Thanks LabNuke99. Turns out it was much simpler than that.... and much more complex. I had a ) at the end of the URL when I created the hyperlink in the blog software. Plus I butchered "systems". into "sytems". Good eye and thanks for catching it and-- reading my blog! Now I'm off to correct the other posted links.
    5,335 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: