VBScript getting results from Shell

后端 未结 4 1347
囚心锁ツ
囚心锁ツ 2020-12-01 21:26
Set wshShell = WScript.CreateObject (\"WSCript.shell\")
wshshell.run \"runas ...\"

How do I get the results and display in a MsgBox

4条回答
  •  青春惊慌失措
    2020-12-01 22:17

    The solution of BoffinBrain still doesn't work, since exec.Status doesn't return an error level (returns just 0 while running and 1 when finished). For that purpose you must use exec.ExitCode (Returns the exit code set by a script or program run using the Exec() method.). So the solution changes to

    Option Explicit
    
    Const WshRunning = 0
    ' Const WshPassed = 0    ' this line is useless now
    Const WshFailed = 1
    
    Dim shell : Set shell = CreateObject("WScript.Shell")
    Dim exec : Set exec = shell.Exec("ping.exe 127.0.0.1 -n 1 -w 500")
    
    While exec.Status = WshRunning
        WScript.Sleep 50
    Wend
    
    Dim output
    
    If exec.ExitCode = WshFailed Then
        output = exec.StdErr.ReadAll
    Else
        output = exec.StdOut.ReadAll
    End If
    
    WScript.Echo output
    

提交回复
热议问题