Obtain the list of TS Session ID

I'm going to create an utility that will purge "unmapped" terminal service printers, I need to obtain the list of Terminal Server Active Session ID so I can check if the printers are related to a disconnected session.

This is the VBScript code

Dim ArrayID()
Dim i

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c QWINSTA.EXE")
i = 1
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Active rdpwd") > 0 Then
If Instr(strText, "rdp-tcp#") > 0 then
start = instr(strtext, "Active rdpwd") - 5
redim preserve ArrayID(i)
ArrayID(i-1) = cint(mid(strtext, start, 3))
i = i +1
End if
End If
For i = 0 To UBound(ArrayID) - 1
wscript.echo ArrayID(i)
Tags: , , , ,

About author

Vittorio Pavesi