2011-02-03 74 views
4

我有下面的VBScript:如何使用VBScript Exec時顯示DOS輸出

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

這將導致一個DOS窗口出現,但不顯示從plink.exe輸出。有沒有辦法讓DOS窗口顯示這個輸出?

回答

3

的Windows腳本宿主缺乏系統()命令,所以你必須實現自己的,恕我直言,我的助手功能優於stealthyninja的版本,因爲它等待的過程和退出不只是空洞的標準輸出,同時也處理標準錯誤:

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

看起來除了ReadAll好阻止,應該是的ReadLine我想 – 2011-02-03 19:22:17

4

@JC:嘗試 -

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

Set oStdOut = Shell.StdOut 

While Not oStdOut.AtEndOfStream 
    sLine = oStdOut.ReadLine 
    WScript.Echo sLine 
Wend 
相關問題