开发者

How to show DOS output when using vbscript Exec

I have the following VBScript:

Set Shell = WScript.CreateObject("WScript.Shell")
commandLine =开发者_StackOverflow 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?


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%\*")


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


The correct way is :

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

Or:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜