ayush
ayush

Reputation: 14568

Execute Process and Redirect Output

I run a process using VBScript. The process usually takes 5-10 minutes to finish and if i run the process standalone then it gives intermittent output while running.

I want to acheive the same logic while running the process using VBScript. Can some one please tell me how to do that?

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

Upvotes: 0

Views: 1559

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

You don't have access to the output of processes started via WMI. What you could do is redirect the output to a file:

result = Process.Create("cmd /c " & command & " >C:\out.txt", , , intProcessID)

and read the file periodically:

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

The above is assuming the process is running on the local host. If it's running on a remote host you need to read the file from a UNC path.

For local processes the Exec method would be an alternative, since it gives you access to the process' 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

Upvotes: 2

Related Questions