My VBScript does not show the results of any command I execute. I know the command gets executed but I would like to capture the result.
我的VBScript没有显示我执行的任何命令的结果。我知道命令被执行了,但是我想要捕获结果。
I have tested many ways of doing this, for example the following:
我已经测试了很多这样做的方法,例如:
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
But it dos not print any results. How do I capture StdOut
and StdErr
?
但它不打印任何结果。如何捕获StdOut和StdErr?
2 个解决方案
#1
4
WScript.Shell.Exec()
returns immediately, even though the process it starts does not. If you try to read Status
or StdOut
right away, there won't be anything there.
shell . exec()立即返回,即使它启动的进程没有返回。如果你试着马上读取状态或StdOut,就不会有任何东西了。
The MSDN documentation suggests using the following loop:
MSDN文档建议使用以下循环:
Do While oExec.Status = 0
WScript.Sleep 100
Loop
This checks Status
every 100ms until it changes. Essentially, you have to wait until the process completes, then you can read the output.
它每100毫秒检查一次状态,直到它发生变化。本质上,您必须等到流程完成后,才能读取输出。
With a few small changes to your code, it works fine:
只需对代码做一些小小的修改,它就可以正常工作:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = WshRunning
WScript.Sleep 100
Loop
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
#2
0
You should read both streams INSIDE the loop as well as after it. When your process is verbose then it will block on the I/O buffer when this buffer will not be emptyfied succesively!!!
您应该在循环内部和循环之后读取这两个流。当您的进程是冗长的时候,它将阻塞在I/O缓冲区中,当这个缓冲区不会被成功地清空!!!
#1
4
WScript.Shell.Exec()
returns immediately, even though the process it starts does not. If you try to read Status
or StdOut
right away, there won't be anything there.
shell . exec()立即返回,即使它启动的进程没有返回。如果你试着马上读取状态或StdOut,就不会有任何东西了。
The MSDN documentation suggests using the following loop:
MSDN文档建议使用以下循环:
Do While oExec.Status = 0
WScript.Sleep 100
Loop
This checks Status
every 100ms until it changes. Essentially, you have to wait until the process completes, then you can read the output.
它每100毫秒检查一次状态,直到它发生变化。本质上,您必须等到流程完成后,才能读取输出。
With a few small changes to your code, it works fine:
只需对代码做一些小小的修改,它就可以正常工作:
Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = WshRunning
WScript.Sleep 100
Loop
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
#2
0
You should read both streams INSIDE the loop as well as after it. When your process is verbose then it will block on the I/O buffer when this buffer will not be emptyfied succesively!!!
您应该在循环内部和循环之后读取这两个流。当您的进程是冗长的时候,它将阻塞在I/O缓冲区中,当这个缓冲区不会被成功地清空!!!