2012-04-01 138 views
0

我想適應一個VBscript運行QWINSTA命令對我已定義爲一個數組文本文件並顯示在文本文件中的結果。VB管道從cmd.exe輸出到STDOUT輸出到寫入文本框

在看了Stack和其他網站上的很多例子之後,這是我最近的一次迭代,它顯示除STDOUT以外的所有內容,這真的是我所追求的,不知道這是我如何調用命令的問題管道輸出或什麼。我也通過兩個變量作爲參數使事情變得複雜:)

在FOR循環完成後,stdout被傳送到的文本文件爲空 我可能要走很長的路,關於我過去的大部分經驗這是批

For Each server In RemotePC 

     'The Query is launched 
     Dim Query As New ProcessStartInfo 

     Query.WorkingDirectory = "c:\" 
     Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt" 
     Query.FileName = "QWINSTA" 
     Query.WindowStyle = ProcessWindowStyle.Hidden 
     Process.Start(Query) 

     'Call Shell("QWINSTA /SERVER:" & server & " " & profile & " >C:\Windows\Temp\SFOUT.txt", vbHidden, Timeout:=5000) 


     Dim SFOUT As New IO.StreamReader("C:\windows\temp\SFOUT.txt") 

     'Results are Echoed to TextboxResults 
     TextBoxResults.Text &= "__________________________________________________________" & ControlChars.CrLf 
     TextBoxResults.Text &= server & ControlChars.CrLf 
     TextBoxResults.Text &= SFOUT.ReadToEnd & ControlChars.CrLf 
     SFOUT.Close() 

    Next 

以下是在VBScript運行的代碼看起來像

For Each server In RemotePC 


Dim WshShell, oExec 
Set WshShell = CreateObject("WScript.Shell") 
Set StrDir = "QWINSTA /SERVER:" & server & " " & profile 


'The Query is launched 
Set oExec = WshShell.Exec(StrDir) 


'We wait for the end of process 
Do While oExec.Status = 0 
WScript.Sleep 500 
Loop 

'We scan and only display the command output of commands without NULL output 
Do While oExec.StdOut.AtEndOfStream <> True 
     Results.WriteLine("__________________________________________________________") 
     Results.WriteLine(server) 
     Results.WriteLine oExec.StdOut.ReadLine 
Loop 



NEXT 

任何幫助表示讚賞

@Nilpo

這是我回來

SESSIONNAME  USERNAME     ID STATE TYPE  DEVICE 

控制檯馬特·1活動

我已經建立了類似這樣使用QWINSTA和一些管道批量輸出,並將其完美的作品,只是有問題適應這一點。


這裏是我試過我試圖通過調用的東西一樣基本的notepad.exe,甚至試圖定義環境變量想着它不讀%PATH%把事情簡單化的最後一件事,在回答@Nilpo

Dim Query As New Process 

     Query.StartInfo.FileName = "notepad.exe" 
     'Query.StartInfo.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt" 
     Query.StartInfo.UseShellExecute = False 
     Query.StartInfo.RedirectStandardOutput = True 
     Query.StartInfo.WindowStyle = ProcessWindowStyle.Normal 

     Environment.SetEnvironmentVariable("PATH", "%PATH%;" & "C:\Windows\System32\notepad.exe") 

     Query.Start() 
+0

如果直接從命令提示符運行命令,文本文件是否正確填充? – Nilpo 2012-04-02 16:29:59

+0

@Nilpo是的沒有問題,如果我運行qwinsta /服務器:localhost亞特> c:\ windows \ temp \ sfout。txt,我找回SESSIONNAME USERNAME ID狀態類型DEVICE 控制檯Matt 1 Active – 2012-04-04 06:59:05

+0

'vb'標記已過時,因爲它不明確而被取消。改用'vb6','vbscript','vba'或'vb.net'之一。 – 2012-04-04 13:47:11

回答

0

輸出重定向後應該有一個空格。

Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt" 

應該

Query.Arguments = server & " " & profile & " > C:\Windows\Temp\SFOUT.txt" 

話雖這麼說,我唯一可以看到,可能不工作就在這裏。

Query.WorkingDirectory = "c:\"  
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt"  
Query.FileName = "QWINSTA"  
Query.WindowStyle = ProcessWindowStyle.Hidden  
Process.Start(Query) 

我不確定你能否以這種方式使用參數。當輸出重定向不是真正的時候,它將被讀作文字參數。在命令行環境中,它被解釋爲單獨的命令,而不是第一個命令的參數。這種方式非常類似於管道。在代碼中提供這個參數可能無法按預期工作。您可能想要返回到Call Shell方法。這將執行命令,就像它在命令行上一樣。

[編輯]

我說得對。這是使用你的方法做到這一點的正確方法。

Query.WorkingDirectory = "c:\" 
Query.Arguments = server & " " & profile & " >C:\Windows\Temp\SFOUT.txt" 
Query.FileName = "QWINSTA" 
Query.WindowStyle = ProcessWindowStyle.Hidden 

'Redirect output 
Query.UseShellExecute = False 
Query.RedirectStandardOutput = True 

Process.Start(Query) 

'Read the output 
Dim output As String = Process.StandardOutput.ReadToEnd 
' wait for the process to terminate 
Process.WaitForExit 
+0

一旦我有一段時間了,我會做出編輯並嘗試一下,最近我一直在使用HTA/VBScript安裝程序項目,謝謝你的回覆,我會告訴你它是否可以工作 – 2012-04-10 19:42:53

+0

請做。我很想聽聽這個如何解決你的問題。 – Nilpo 2012-04-10 22:57:19

+0

仍然沒有那裏,不管我得到什麼「系統找不到指定的文件」MSDN在使用'code'時說,UseShellExecute =假'代碼',工作目錄幾乎被忽略,所以有必要聲明完整的路徑文件名,所以我做到了,但無論我如何格式化,都失敗了。我會在頂部發佈一個代碼更新,我嘗試了一些東西 – 2012-04-13 08:46:04