テキスト ファイルからプルし、QWINSTA コマンドを実行しているサーバーのリストを調べて、特定のユーザーのターミナル サービス セッションを探す VBA アプリケーションを作成しました。これは、ヘルプデスクがユーザー名を入力してボタンをクリックできるという考え方です。サーバーのリストをすばやくクロールし、誰かがログオンしている場所を特定します。
何らかの理由で、ループの最後に、書き込み先のテキストボックスに奇妙な出力が表示されます。これは、クエリデータのように見えますが、適切な形式ではなく、数回繰り返されます
これがコードです。「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