How can I use a VBScript script to ping a machine?

John Savill

November 13, 2005

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

A. The Windows Management Instrumentation (WMI) classes in Windows XP and later provide a Win32_PingStatus object that you can use to ping a machine. The following script, which you can download at http://www.windowsitpro.com/content/content/48449/vbpinging.zip uses this object to ping a passed hostname or IP address. Because of space constraints, some lines wrap to two lines.

Option ExplicitDim strHost' Check that all arguments required have been passed.If Wscript.Arguments.Count 0 then    Ping = False            'WScript.Echo "Status code is " & objRetStatus.StatusCode        else            Ping = True            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize            'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive        end if    nextEnd Function 

You can modify this script to do whatever you need. Notice that I've commented out some lines (') that give more information about the ping attempt, but you can leave the lines in if the information would be useful to you. Run the script by using the following command:

D:projectsVBScripts>cscript vbping.vbs savdalex01

which will give the following sample output:

Host savdalex01 contacted

You can find more information about Win32_PingStatus at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp .

About the Author

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