我做了一個VBA應用程序,該應用程序從文本文件中提取並遍歷運行QWINSTA命令的服務器列表,以查找特定用戶的終端服務會話,是我們的服務檯可以輸入用戶名並單擊一個按鈕,它可以快速檢索服務器列表並識別某人登錄的位置。VB在循環結束時獲取垃圾文本框輸出
由於某種原因,在我的循環結束時,我得到它寫入,似乎是在查詢數據文本框中一些奇怪的輸出,但不能以正確的格式,反覆幾次
這裏的代碼,在結尾處添加了「END OF OUTPUT」行,以確定垃圾數據是否在該行之後,並且似乎在之前發生,因爲該行在退出循環後寫入。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Creates Thread
Dim T1 As New System.Threading.Thread(AddressOf SessionFinder)
'Start Thread
T1.Start()
LabelStatus.Text = "Running..."
End Sub
'SessionFinder 2.0 - searches for RDP Sessions on terminal servers
Sub SessionFinder()
Control.CheckForIllegalCrossThreadCalls = False
ProgressBar1.Value = 0
'declares variables
Dim objFSO, objShell, profile, server, oFSO, WSHshell, oTextStream, RemotePC
'Inputbox that collects username
profile = TextBoxProfile.Text
'Creates Class Objects
objFSO = CreateObject("Scripting.FileSystemObject")
objShell = CreateObject("Wscript.Shell")
TextBoxResults.Text = "RDP Sessions for user " & profile & " exists on the following servers" & ControlChars.CrLf
'TextBoxResults.Text &= "_______________________________________________________________________________" & ControlChars.CrLf
TextBoxResults.Text &= " " & ControlChars.CrLf
'open the file system object
oFSO = CreateObject("Scripting.FileSystemObject")
WSHshell = CreateObject("wscript.shell")
'open the data file
oTextStream = oFSO.OpenTextFile("phlist.txt")
'make an array from the data file
RemotePC = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close()
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 100
ProgressBar1.Step = 1
For Each server In RemotePC
Label8.Text = server
'The Query is launched
Dim Query As New Process
Query.StartInfo.FileName = "C:\windows\system32\qwinsta.exe"
Query.StartInfo.Arguments = "/server:" & server & " " & profile
Query.StartInfo.UseShellExecute = False
Query.StartInfo.RedirectStandardOutput = True
Query.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
Query.StartInfo.CreateNoWindow = True
Query.Start()
Query.WaitForExit(3000)
If Query.HasExited = False Then
TextBoxResults.Text &= "_________________________________________________________________" & ControlChars.CrLf
TextBoxResults.Text &= server & " Not Responding, skipping..."
Query.Kill()
Else
'Do Nothing
End If
Dim output As String = Query.StandardOutput.ReadToEnd
If Not String.IsNullOrEmpty(output) Then
'MsgBox("/server:" & server & " " & profile)
'MsgBox(output)
'Results are Echoed to TextboxResults
TextBoxResults.Text &= "_________________________________________________________________" & ControlChars.CrLf
TextBoxResults.Text &= server & ControlChars.CrLf
TextBoxResults.Text &= output
Else
'Do nothing
End If
output = Nothing
ProgressBar1.PerformStep()
Next
LabelStatus.Text = "Complete"
TextBoxResults.Text &= "_________________________________________________________________" & ControlChars.CrLf
TextBoxResults.Text &= "END OF OUTPUT"
ProgressBar1.Value = 100
Me.BringToFront()
End Sub
這裏是什麼樣的輸出應該看起來像
RDP Sessions for user amis5235 exists on the following servers
_________________________________________________________________
XDIS1
SESSIONNAME USERNAME ID STATE TYPE DEVICE
ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
XAXCL4 Not Responding, skipping...
_________________________________________________________________
XAXCL6 Not Responding, skipping...
_________________________________________________________________
這裏是結束了尾隨結束,我想這消除
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>ica-tcp#4 amis5235 6 Active wdica
_________________________________________________________________
END OF OUTPUT
似乎RemotePC陣列有重複。在For循環之前嘗試MsgBox RemotePC.Count。它代表了正確數量的服務器嗎? – Ahmad
'Control.CheckForIllegalCrossThreadCalls = False'意思是「我喜歡弄垃圾,把它打開!」 –
你指出我的方向,帶來了一個答案,我很感謝你,你對重複的評論導致我仔細檢查我的文本文件,似乎在最後一次輸入後有一些回車,看起來像它試圖處理空行,清理文本文件,修復了問題 –