VBScript getting results from Shell

Cocoa Dev picture Cocoa Dev · May 19, 2011 · Viewed 71.3k times · Source
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "runas ..."

How do I get the results and display in a MsgBox

Answer

Nilpo picture Nilpo · May 20, 2011

You will want to use the WshShell object's Exec method instead of Run. Then simply read the command line's output from the standard streams. Try this one:

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
End Select

WScript.StdOut.Write strOutput  'write results to the command line
WScript.Echo strOutput          'write results to default output
MsgBox strOutput                'write results in a message box