我可以成功运行test.vbs,语法为:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\uncpathfile.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
end with
但是,我想将输出存储到\uncpath%computername%.txt
这不起作用:
sEXE = """\uncpathfile.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 = """\uncpathfile.exe"""
with CreateObject("WScript.Shell")
.Run sEXE & " >>\uncpath%computername%.txt", 1, true ' Wait for finish or False to not wait
end with
有什么帮助吗?
.Run()
方法无法读取使用.Exec()
的任务的标准输出,但您需要进行一些更改来模拟.Run()
自动为您执行的阻塞。
Dim WshShell, sEXE, cmd, result
Set WshShell = CreateObject("WScript.Shell")
sEXE = """\uncpathfile.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 ""\uncpathfile.exe >> \uncpath%computername%.txt"""
With CreateObject("WScript.Shell")
.Run sEXE & " ", 1, true ' Wait for finish or False to not wait
End With
有用的链接
- WshScriptExec对象(
.Exec()
返回) - TextStream对象(由
.StdIn
、StdOut
和StdErr
返回)