0

I made a VBA application that pulls from a text file, and goes through a list of servers running the QWINSTA command against them to look for Terminal Services sessions for a particular user, the idea is our helpdesk can type in a username and click a button and it crawls a list of servers, quickly and identifies where someone is logged on.

for some reason at the end of my loop I get some odd output in the textbox it writes to that appears to be the query data, but not in the proper format, repeated several times

here's the code, the "END OF OUTPUT" line was added to the end to determine if the garbage data was before of after this line and it seems to occur before, because this line writes after the loop has been exited.

  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

Here's a sample of what the output is supposed to look like

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...
_________________________________________________________________

Here's what ends up trailing on the end, which I'd like to eliminate

_________________________________________________________________

 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
4

2 回答 2

0

好的,我现在几乎要笑了,Ahmad 的评论提示我检查我正在阅读的文本文件,查看文件末尾有许多空回车,好像我的应用程序正在尝试处理空行作为服务器,这现在很有意义。感谢大家的共鸣板,正是我所需要的。

于 2012-11-05T17:31:16.050 回答
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

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

于 2012-11-05T12:01:00.083 回答