How to show DOS output when using vbscript Exec

32,366

Solution 1

Windows scripting host lacks a system() command so you have to implement your own, IMHO my helper function is superior to stealthyninja's version since it waits for process exit and not just empty stdout and it also handles stderr:

Function ExecuteWithTerminalOutput(cmd)
Set sh = WScript.CreateObject("WScript.Shell")
Set exec =  sh.Exec(cmd)
Do While exec.Status = 0
    WScript.Sleep 100
    WScript.StdOut.Write(exec.StdOut.ReadAll())
    WScript.StdErr.Write(exec.StdErr.ReadAll())
Loop
ExecuteWithTerminalOutput = exec.Status
End Function


call ExecuteWithTerminalOutput("cmd.exe /c dir %windir%\*")

Solution 2

Try --

Set Shell = WScript.CreateObject("WScript.Shell")
commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]    
Set oExec = Shell.Exec(commandLine)

Set oStdOut = oExec.StdOut

While Not oStdOut.AtEndOfStream
    sLine = oStdOut.ReadLine
    WScript.Echo sLine
Wend
Share:
32,366
JC.
Author by

JC.

Updated on September 25, 2020

Comments

  • JC.
    JC. over 3 years

    I have the following VBScript:

    Set Shell = WScript.CreateObject("WScript.Shell")
    commandLine = puttyPath & "\plink.exe -v" & " -ssh" [plus additional commands here]    
    Set oExec = Shell.Exec(commandLine)
    

    This causes a DOS window to appear but the output from plink.exe is not displayed. Is there any way to get the DOS window to display this output?

  • JC.
    JC. over 13 years
    Looks good except ReadAll is blocking, should be ReadLine I think
  • Anders
    Anders over 13 years
    @JS: yes it is blocking and you could probably change it to WriteLine+ReadLine as long as the output is text based and not part of some kind of binary pipe operation.
  • Anders
    Anders over 10 years
    @Ghigo: Paste into a file called test.vbs and run cscript.exe test.vbs Worked correctly on XP when I wrote this answer and I just tested on a Windows 8 machine and it worked there as well...
  • Ghigo
    Ghigo over 10 years
    @Anders: Paste into a file called test.vbs and double click to run with wscript.exe (default handler for vbs files) and you get an invalid handle message at line 6 char 5. I made my version and I check Do While Not objExecObject.StdOut.AtEndOfStream instead of exec.Status=0 to determine child process end.