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