I need to retrieve usernames of all users logged onto a Windows 2003 terminal server. The script below is posted on several sites, purporting to do just that. However, whilst the first WMI query does return the logon sessions, the
second returns nothing, so I cannot get the username. If strComputer is a Windows XP desktop, usernames are returned.
[tt]strComputer = "tserver"
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 10") ' 10=RDP
Wscript.Echo colSessions.Count & " users found" ' This bit worked
If colSessions.Count <> 0 Then
WScript.Echo "RDP Sessions:"
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objItem in colList
WScript.Echo "Username: " & objItem.Name & VBCRLF &_
"FullName: " & objItem.FullName ' This bit returns nothing
Next
Next
End If[/tt]
Any ideas what is wrong?
second returns nothing, so I cannot get the username. If strComputer is a Windows XP desktop, usernames are returned.
[tt]strComputer = "tserver"
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 10") ' 10=RDP
Wscript.Echo colSessions.Count & " users found" ' This bit worked
If colSessions.Count <> 0 Then
WScript.Echo "RDP Sessions:"
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objItem in colList
WScript.Echo "Username: " & objItem.Name & VBCRLF &_
"FullName: " & objItem.FullName ' This bit returns nothing
Next
Next
End If[/tt]
Any ideas what is wrong?