2012-11-05 75 views
0

我做了一個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 
+0

似乎RemotePC陣列有重複。在For循環之前嘗試MsgBox RemotePC.Count。它代表了正確數量的服務器嗎? – Ahmad

+0

'Control.CheckForIllegalCrossThreadCalls = False'意思是「我喜歡弄垃圾,把它打開!」 –

+0

你指出我的方向,帶來了一個答案,我很感謝你,你對重複的評論導致我仔細檢查我的文本文件,似乎在最後一次輸入後有一些回車,看起來像它試圖處理空行,清理文本文件,修復了問題 –

回答

0

我的樣本認爲這是因爲線程正在終止。試試這個:

Dim T1 As System.Threading.Thread; 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    'Creates Thread 
    T1 = New System.Threading.Thread(AddressOf SessionFinder) 

    'Start Thread 
    T1.Start() 

    LabelStatus.Text = "Running..." 

End Sub 

注:這只是一個例子而不是生產代碼;)

+0

嘗試了你的改變,似乎沒有什麼區別,不知道它是否重要,但「ping」是作爲一種方式添加的以超時該命令,否則如果列表中的某個服務器關閉,則命令超時和循環自行前進需要非常長的時間,從而破壞了快速工具的用途。我可以理解提前終止的命令有一個奇怪的輸出效果,只是不知道爲什麼,或者如果有更好的方式去查詢它們之前輪詢服務器。 –

0

好的,我幾乎馬上笑,艾哈邁德的評論促使我檢查我的文本文件,我從讀書,看在文件末尾有一些空的回車,好像我的應用程序試圖將空行處理爲服務器,現在這些服務器非常有意義。感謝大家的發聲板,正是我所需要的。