Script to see if someone is logged in via RDP

M

Mike

I have a script that will tell you if a user is physically logged into a
workstation, but if the user RDPs and logs onto the workstation it appears
no one is logged in. Is there a way to tell via a script if someone is
logged in via RDP to an XP workstation. Here is my script that works for a
physical log in.


On Error Resume Next

Dim name

Dim User(20)
Dim wks(20)
Dim comname(20)
Dim strComputer(20)

y = 0

For x = 1 to 9

if y < 9 then
name = "computername-0"
else
name = "computername-"
end if

y = y + 1
comname(x) = name & y
Strcomputer(x) = comname(x)

Set objWMIService = GetObject("winmgmts:\\" & strComputer(x) &
"\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_ComputerSystem",,48)
For Each objItem in colItems
wks(x) = objItem.Name
user(x) = objItem.UserName
Next

Next

For r = 1 to 9

if user(r) <> vbempty then
Wscript.Echo wks(r) & " is currently in use by " & user(r)
else
Wscript.Echo wks(r) & " is available for RDP"
end if

Next
 
T

Torgeir Bakken \(MVP\)

Mike said:
I have a script that will tell you if a user is physically logged into a
workstation, but if the user RDPs and logs onto the workstation it appears
no one is logged in. Is there a way to tell via a script if someone is
logged in via RDP to an XP workstation. Here is my script that works for
a physical log in.
Hi

In the script in the link below,

change
"Select * from Win32_LogonSession Where LogonType = 2"

to
"Select * from Win32_LogonSession Where LogonType = 10"

http://groups.google.co.uk/[email protected]


Alternatively, parse the output from the following command:

QWINSTA.EXE /SERVER:<some computer>

Look for lines with the text "RDP-Tcp#" in it, it will indicate a RDP
session and the same line will also contain the user account name.

Here is a VBScript example (you need to remove the "console" part
from the command line):
http://groups.google.co.uk/groups?h...m=#[email protected]&rnum=5
 
M

Mike

The Script below works great, but it pulls the console user, I need it to
pull the username from the "RDP-Tcp#" is this possible if so how. The
overall goal we are trying to accomplish is that we have multiple blade
workstations with XP pro loaded on them, we are trying to use them for RDP
purposes. So we are trying to have a web page to display if a user is RDP
into one of those blades and if not have a button to connect the user the
RDP session so they can logon.

Set oWshNet = CreateObject("WScript.Network")
' use current computer
sComputerName = oWshNet.ComputerName

sUserName = GetConsoleUser(sComputerName)
If sUserName <> "" Then
MsgBox "Console user name: " & sUserName
Else
MsgBox "Console user name not found with QWINSTA.EXE"
End If

Function GetConsoleUser(sHost)
' Function will return console user name from QWINSTA.EXE
' Windows XP and Win2k3 Server only

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTempFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command via Command Prompt and use for /f to extract user name
'Dump results into a temp text file
oShell.Run "%ComSpec% /c for /f ""skip=1 Tokens=2"" %i in " _
& "('%SystemRoot%\System32\QWINSTA.EXE console /SERVER:" _
& sHost & "') do echo %i >" & sTempFile, 0, True

GetConsoleUser = "" 'init value

If oFS.FileExists(sTempFile) Then
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile)

'Parse the text file
Do While Not oTF.AtEndOfStream
GetConsoleUser = Trim(oTF.ReadLine)
Loop

'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
End If

End Function

Thanks, Mike
 
T

Torgeir Bakken \(MVP\)

Mike said:
The Script below works great, but it pulls the console user, I need it to
pull the username from the "RDP-Tcp#" is this possible if so how. The
overall goal we are trying to accomplish is that we have multiple blade
workstations with XP pro loaded on them, we are trying to use them for RDP
purposes. So we are trying to have a web page to display if a user is RDP
into one of those blades and if not have a button to connect the user the
RDP session so they can logon.
Hi

Script below is based on the following different outputs from
QWINSTA.EXE, and will return any logged on user name regardless
of RDP or console logon):


Not able to connect to computer (NB: Output goes to StdErr and not
StdOut in this case):

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
Error opening Terminal server ABCDEF
Error [1722]:The RPC server is unavailable.


Nobody logged on:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp 65536 Listen rdpwd


Nobody logged on, an RDP user have logged off last:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
0 Disc rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Console logon by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console MyUserNameHere 0 Active wdcon
rdp-tcp 65536 Listen rdpwd


RDP logon session by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
rdp-tcp#3 MyUserNameHere 0 Active rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Disconnected RDP logon session by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
MyUserNameHere 0 Disc rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Another RDP user is about to log off MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
rdp-tcp#3 MyUserNameHere 0 Active rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon
rdp-tcp#5 3 Conn rdpwd




'--------------------8<----------------------

sComputerName = "ABCDEF"

sUserName = GetUser(sComputerName)

If IsEmpty(sUserName) Then
WScript.Echo "Could not connect to computer " & sComputerName
ElseIf sUserName = "" Then
WScript.Echo "No user is logged on"
Else
WScript.Echo "Logged on user name: " & sUserName
End If


Function GetUser(sHost)
' Function will return logged in user name from QWINSTA.EXE
' regardless of it is a console or RDP user
' Windows XP and Win2k3 Server only
'
' Return values:
' If input to function (host name) is empty, returns Empty
' If QWINSTA.EXE cannot connect to host, returns Empty
' If no user is listed by QWINSTA.EXE, returns ""
' If user is listed, returns user name

If sHost = "" Then
Exit Function '-----> return Empty
End if

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTmpFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command and redirect stdout and stderr into temp file
oShell.Run "%ComSpec% /c %SystemRoot%\System32\QWINSTA.EXE /SERVER:" _
& sHost & " >" & sTmpFile & " 2>&1", 0, True

On Error Resume Next
'Open the temp file
Set oTF = oFS.OpenTextFile(sTmpFile)

'Parse the file

' Read first line
sLine = oTF.ReadLine
If Err.Number <> 0 Then
' Something is wrong.
Exit Function '-----> return Empty
End If
On Error Goto 0

If Left(Trim(sLine), 26) <> "SESSIONNAME USERNAME" Then
' Something is wrong. Most likely is the content of the first line
' this: "Error opening Terminal server <host name>"
Exit Function '-----> return Empty
End If

' Read second line
sLine = oTF.ReadLine
'Close file
oTF.Close
'Delete it
oFS.DeleteFile sTmpFile

sChoppedLine = Mid(sLine, 20)
If Left(sChoppedLine, 1) = " " Then
' no user found
GetUser = ""
Else
' get the user name
GetUser = Split(sChoppedLine)(0)
End If

End Function

'--------------------8<----------------------
 
M

Mike

The script works great, but is there a way to the script pull a list of
computernames from a text file and have your script run against that list.
I have a script (below) to read from a text file but I can't seem to merge
it with your script.

On Error Resume Next

Dim fso, f, inputFile, strPC, strCompName

inputFile = "C:\computers.txt"

set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(inputFile)

Do While f.AtEndOfLine <> True
strPC = f.ReadLine
strCompName = UCase(strPC)
wscript.echo "test loop " & Date & " at " & time & " "
& strCompName


Loop

f.close

wscript.echo "end of file"

Torgeir Bakken (MVP) said:
Mike said:
The Script below works great, but it pulls the console user, I need it to
pull the username from the "RDP-Tcp#" is this possible if so how. The
overall goal we are trying to accomplish is that we have multiple blade
workstations with XP pro loaded on them, we are trying to use them for
RDP purposes. So we are trying to have a web page to display if a user
is RDP into one of those blades and if not have a button to connect the
user the RDP session so they can logon.
Hi

Script below is based on the following different outputs from
QWINSTA.EXE, and will return any logged on user name regardless
of RDP or console logon):


Not able to connect to computer (NB: Output goes to StdErr and not
StdOut in this case):

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
Error opening Terminal server ABCDEF
Error [1722]:The RPC server is unavailable.


Nobody logged on:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console 0 Conn wdcon
rdp-tcp 65536 Listen rdpwd


Nobody logged on, an RDP user have logged off last:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
0 Disc rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Console logon by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
console MyUserNameHere 0 Active wdcon
rdp-tcp 65536 Listen rdpwd


RDP logon session by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
rdp-tcp#3 MyUserNameHere 0 Active rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Disconnected RDP logon session by user MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
MyUserNameHere 0 Disc rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon


Another RDP user is about to log off MyUserNameHere:

C:\WINDOWS\SYSTEM32>QWINSTA.EXE /SERVER:ABCDEF
SESSIONNAME USERNAME ID STATE TYPE DEVICE
rdp-tcp#3 MyUserNameHere 0 Active rdpwd
rdp-tcp 65536 Listen rdpwd
console 2 Conn wdcon
rdp-tcp#5 3 Conn rdpwd




'--------------------8<----------------------

sComputerName = "ABCDEF"

sUserName = GetUser(sComputerName)

If IsEmpty(sUserName) Then
WScript.Echo "Could not connect to computer " & sComputerName
ElseIf sUserName = "" Then
WScript.Echo "No user is logged on"
Else
WScript.Echo "Logged on user name: " & sUserName
End If


Function GetUser(sHost)
' Function will return logged in user name from QWINSTA.EXE
' regardless of it is a console or RDP user
' Windows XP and Win2k3 Server only
'
' Return values:
' If input to function (host name) is empty, returns Empty
' If QWINSTA.EXE cannot connect to host, returns Empty
' If no user is listed by QWINSTA.EXE, returns ""
' If user is listed, returns user name

If sHost = "" Then
Exit Function '-----> return Empty
End if

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTmpFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command and redirect stdout and stderr into temp file
oShell.Run "%ComSpec% /c %SystemRoot%\System32\QWINSTA.EXE /SERVER:" _
& sHost & " >" & sTmpFile & " 2>&1", 0, True

On Error Resume Next
'Open the temp file
Set oTF = oFS.OpenTextFile(sTmpFile)

'Parse the file

' Read first line
sLine = oTF.ReadLine
If Err.Number <> 0 Then
' Something is wrong.
Exit Function '-----> return Empty
End If
On Error Goto 0

If Left(Trim(sLine), 26) <> "SESSIONNAME USERNAME" Then
' Something is wrong. Most likely is the content of the first line
' this: "Error opening Terminal server <host name>"
Exit Function '-----> return Empty
End If

' Read second line
sLine = oTF.ReadLine
'Close file
oTF.Close
'Delete it
oFS.DeleteFile sTmpFile

sChoppedLine = Mid(sLine, 20)
If Left(sChoppedLine, 1) = " " Then
' no user found
GetUser = ""
Else
' get the user name
GetUser = Split(sChoppedLine)(0)
End If

End Function

'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
 
T

Torgeir Bakken \(MVP\)

Mike said:
The script works great, but is there a way to the script pull a list of
computernames from a text file and have your script run against that list.
I have a script (below) to read from a text file but I can't seem to merge
it with your script.
Hi


'--------------------8<----------------------

Dim fso, f, inputFile, strCompName, strUserName

inputFile = "C:\computers.txt"

set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(inputFile)

Do While f.AtEndOfLine <> True
strCompName = Trim(f.ReadLine)
If strCompName <> "" Then

strCompName = UCase(strCompName)
strUserName = GetUser(strCompName)

If IsEmpty(strUserName) Then
WScript.Echo strCompName & " ; Could not connect to computer"
ElseIf strUserName = "" Then
WScript.Echo strCompName & " ; No user is logged on"
Else
WScript.Echo strCompName & " ; Logged on user name: " & strUserName
End If
End If
Loop

f.close

wscript.echo "end of file"


Function GetUser(sHost)
' Function will return logged in user name from QWINSTA.EXE
' regardless of it is a console or RDP user
' Windows XP and Win2k3 Server only
'
' Return values:
' If input to function (host name) is empty, returns Empty
' If QWINSTA.EXE cannot connect to host, returns Empty
' If no user is listed by QWINSTA.EXE, returns ""
' If user is listed, returns user name

If sHost = "" Then
Exit Function '-----> return Empty
End if

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTmpFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command and redirect stdout and stderr into temp file
oShell.Run "%ComSpec% /c %SystemRoot%\System32\QWINSTA.EXE /SERVER:" _
& sHost & " >" & sTmpFile & " 2>&1", 0, True

On Error Resume Next
'Open the temp file
Set oTF = oFS.OpenTextFile(sTmpFile)

'Parse the file

' Read first line
sLine = oTF.ReadLine
If Err.Number <> 0 Then
' Something is wrong.
Exit Function '-----> return Empty
End If
On Error Goto 0

If Left(Trim(sLine), 26) <> "SESSIONNAME USERNAME" Then
' Something is wrong. Most likely is the content of the first line
' this: "Error opening Terminal server <host name>"
Exit Function '-----> return Empty
End If

' Read second line
sLine = oTF.ReadLine
'Close file
oTF.Close
'Delete it
oFS.DeleteFile sTmpFile

sChoppedLine = Mid(sLine, 20)
If Left(sChoppedLine, 1) = " " Then
' no user found
GetUser = ""
Else
' get the user name
GetUser = Split(sChoppedLine)(0)
End If

End Function
'--------------------8<----------------------
 
J

John Whitmer

Hello,

Is there a way to have this script work in an ASP webpage? I tried it and
the all the systems came back as "Could not connect to computer " But yet I
run the script from dos it works just fine. here is what's in the temp
file that the web server creates..


Error opening Terminal server Computer-01
Error [5]:Access is denied.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top