vbscript .运行 显示输出

vbscript .run show output

我可以 运行 成功 test.vbs 语法:

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

sEXE = """\uncpath\file.exe"""
with CreateObject("WScript.Shell")
  .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with

但是我想将输出存储到 \\uncpath\%computername%.txt

这不起作用:

sEXE = """\uncpath\file.exe>>\uncpath\%computername%.txt"""
with CreateObject("WScript.Shell")
  .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with

error with line: with CreateObject("WScript.Shell")

这也不行。

sEXE = """\uncpath\file.exe"""
with CreateObject("WScript.Shell")
  .Run sEXE & " >>\uncpath\%computername%.txt", 1, true ' Wait for finish or False to not wait
end with

有什么帮助吗?

.Run() 方法无法从您使用 .Exec() 的任务中读取标准输出,但您需要进行一些更改以模拟 [=12] 的阻塞=] 自动为您完成。

Dim WshShell, sEXE, cmd, result
Set WshShell = CreateObject("WScript.Shell")

sEXE = """\uncpath\file.exe"""
With CreateObject("WScript.Shell")
  Set cmd = .Exec(sEXE)
  'Block until complete.
  Do While cmd.Status <> 1
     WScript.Sleep 100
  Loop
  'Get output
  result = cmd.StdOut.Readall()
  'Check the output
  WScript.Echo result
  Set cmd = Nothing
End With

另一种方法是在 sEXE 变量前添加前缀,这样您就可以使用 cmd /c (因为 >> 命令是其中的一部分).

这应该有效

sEXE = "cmd /c ""\uncpath\file.exe >> \uncpath\%computername%.txt"""
With CreateObject("WScript.Shell")
  .Run sEXE & " ", 1, true ' Wait for finish or False to not wait
End With

有用的链接