我使用VBScript运行一个进程。这个过程通常需要5-10分钟才能完成,如果我独立运行该过程,那么它在运行时会产生间歇输出。
我想在使用VBScript运行进程时实现相同的逻辑。有人可以告诉我该怎么做?
Set Process = objSWbemServices.Get("Win32_Process")
result = Process.Create(command, , , intProcessID)
waitTillProcessFinishes objSWbemServices,intProcessID
REM How to get the output when the process has finished or while it is running
答案 0 :(得分:2)
您无权访问通过WMI启动的进程的输出。您可以做的是将输出重定向到文件:
result = Process.Create("cmd /c " & command & " >C:\out.txt", , , intProcessID)
并定期阅读文件:
Set fso = CreateObject("Scripting.FileSystemObject")
linesRead = 0
qry = "SELECT * FROM Win32_Process WHERE ProcessID=" & intProcessID
Do While objSWbemServices.ExecQuery(qry).Count > 0
Set f = fso.OpenTextFile("C:\out.txt")
Do Until f.AtEndOfStream
For i = 1 To linesRead : f.SkipLine : Next
WScript.Echo f.ReadLine
linesRead = linesRead + 1
Loop
f.Close
WScript.Sleep 100
Loop
以上假设该进程正在本地主机上运行。如果它在远程主机上运行,则需要从UNC路径读取文件。
对于本地流程,Exec
方法可以替代,因为它可以让您访问流程'StdOut:
Set sh = CreateObject("WScript.Shell")
Set p = sh.Exec(command)
Do While p.Status = 0
Do Until p.StdOut.AtEndOfStream
WScript.Echo p.StdOut.ReadLine
Loop
WScript.Sleep 100
Loop
WScript.Echo p.StdOut.ReadAll