2011-02-03 3 views
4

je le VBScript suivant:Comment afficher la sortie DOS lorsque vous utilisez Exec vbscript

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

Cela provoque une fenêtre DOS à apparaître, mais la sortie de plink.exe n'est pas affiché. Est-il possible d'obtenir la fenêtre DOS pour afficher cette sortie?

Répondre

3

hôte de script Windows ne dispose pas d'un système() de sorte que vous devez mettre en œuvre vos propres, à mon humble avis ma fonction d'aide est supérieure à la version stealthyninja car il attend la sortie du processus et non seulement stdout vide et il gère également 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%\*") 
+0

semble bon, sauf ReadAll bloque, devrait être ReadLine je pense –

+0

@JS: oui, il bloque et vous pourriez probablement changer à WriteLine + ReadLine aussi longtemps que la la sortie est basée sur le texte et ne fait pas partie d'une sorte de binaire p opération ipe. – Anders

+0

Ce code ne fonctionne pas. Je me demande pourquoi c'est une réponse acceptée. – Ghigo

4

@JC: Essayez - commande

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 
Questions connexes