Run the Ping command

There are many times when you will want to run external commands from inside VBScript. I generally recommend doing something like this, when VBScript either does not support doing something, or when it is just easier to use the external command. The script below illustrates how to do this.

 

' NAME: <RunPingCommand.vbs>
'
' COMMENT: Key concepts are listed below:
'1. using the Wscript.shell exec method to run programs
'2. using the StdOut method to capture output
'3. using Instr to parse that output and look for stuff
'==========================================================================

Option Explicit ' is used to force the scripter to declare variables
On Error Resume Next ' is used to tell vbscript to go to the next line if it encounters an Error
Dim objShell
Dim objExecObject
Dim strText

WScript.echo "starting program " & Now ' used to mark when program begins
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping 127.0.0.1")

Do until objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo strText
End If
Loop
WScript.echo "complete" ' lets me know program is done running