VB在循环结束时获取垃圾文本框输出

时间:2012-11-05 06:53:14

标签: vb.net

我创建了一个从文本文件中提取的VBA应用程序,并通过一个运行QWINSTA命令的服务器列表来查找特定用户的终端服务会话,我们的帮助台可以输入用户名和单击一个按钮,它会快速抓取服务器列表并识别某人登录的位置。

由于某种原因,在我的循环结束时,我在它写入的文本框中得到一些奇怪的输出,看起来像是查询数据,但没有以正确的格式重复多次

这里是代码," END OF OUTPUT" line被添加到最后,以确定垃圾数据是否在此行之前,之前似乎发生,因为此行在退出循环后写入。

  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

2 个答案:

答案 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

注意:这只是一个示例而非生产代码;)

答案 1 :(得分:0)

好吧,我现在差不多笑了,艾哈迈德的评论促使我查看我正在阅读的文本文件,看着文件的末尾有一些空的回车,看起来像我的应用程序试图将空行处理为服务器,这现在非常有意义。感谢每个人的发声板,正是我所需要的。