【问题标题】:VBScript getting results from ShellVBScript 从 Shell 获取结果
【发布时间】:2011-08-28 23:41:15
【问题描述】:
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "runas ..."

如何获取结果并显示在 MsgBox 中

【问题讨论】:

  • 定义“结果”。 Runas 退出代码?应用程序的退出代码通过 runas 运行?应用程序的控制台输出?

标签: scripting vbscript wsh windows-scripting


【解决方案1】:

BoffinBrain 的解决方案仍然不起作用,因为 exec.Status 不返回错误级别(运行时仅返回 0,完成时返回 1)。为此,您必须使用 exec.ExitCode(返回由使用 Exec() 方法运行的脚本或程序设置的退出代码。)。所以解决方案变为

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

【讨论】:

    【解决方案2】:

    这是 Nilpo 答案的修改版本,修复了 WshShell.Exec 异步的问题。我们做一个繁忙的循环等待,直到 shell 的状态不再运行,然后我们检查输出。将命令行参数-n 1 更改为更高的值以使ping 花费更长的时间,并看到脚本将等待更长的时间直到完成。

    (如果有人有真正的异步、基于事件的解决方案,请告诉我!)

    Option Explicit
    
    Const WshRunning = 0
    Const WshFinished = 1
    Const WshFailed = 2
    
    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.Status = WshFailed Then
        output = exec.StdErr.ReadAll
    Else
        output = exec.StdOut.ReadAll
    End If
    
    WScript.Echo output
    

    【讨论】:

      【解决方案3】:
      var errorlevel = new ActiveXObject('WScript.Shell').Run(command, 0, true)
      

      第三个参数必须为真,errorlevel为返回值,判断是否为0。

      【讨论】:

      • 不是 VBScript;不是(标准)输出(放置)。
      • @Ekkehard.Horner 我用jscript测试代码,我觉得vbscript也可以
      • 从另一个答案中可以看出,您的假设是错误的。
      • @Ekkehard.Horner no,如果第三个参数为true,则返回值为errorlevel
      【解决方案4】:

      您将希望使用 WshShell 对象的 Exec 方法而不是 Run。然后只需从标准流中读取命令行的输出。试试这个:

      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
      

      【讨论】:

      • 我们能用 WshShell.Run 做同样的事情吗?
      • 没有。 Run 不提供对标准流的访问。
      • 注意:这是异步的,因此您可能会在Select Case 看到不正确的WshShellExec.Status
      • @MattBorja 如果您按预期使用它来运行命令行程序,它将运行命令并在完成时返回一个对象。但是,如果您启动诸如 calc.exe 之类的窗口应用程序,您将需要一个循环,因为命令行会在程序执行结束之前返回。在这种情况下,您只需循环直到WshShellExec.Status <> 0
      • @Nilpo 我在运行任何东西时遇到了同样的问题。正如 rdev5 所说,Exec 是异步的,所以WshShellExec.Status 在您第一次检查它时仍然是 0(正在运行)。您需要循环,直到完成类似 While WshShellExec.Status = 0 : WScript.Sleep 50 : Wend 之类的内容,也许可以考虑编辑您的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多