1

我可以使用以下语法成功运行 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

行错误:使用 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

有什么帮助吗?

4

1 回答 1

1

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

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

有用的链接

于 2015-12-28T21:04:46.623 回答