Search code examples
vbscriptwshremote-process

vbscript .run show output


I can run successfully test.vbs with syntax:

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

however I want to store the output to \\\uncpath\%computername%.txt

This doesn't work:

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

error with line: with CreateObject("WScript.Shell")

This doesn't work either.

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

any help?


Solution

  • The .Run() method doesn't have the ability to read the standard output from a task you use .Exec() for that, but you need a few changes to simulate the blocking that .Run() does for you automatically.

    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
    

    The other approach is to prefix the sEXE variable so you are using cmd /c (as the >> command is part of that).

    This should work

    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
    

    Useful Links