Advertisement

10.22.2008 at 09:54AM PDT, ID: 23837732 | Points: 500
[x]
Attachment Details

How to use WMI to list remote users on a terminal server

I am attempting to write a script that enumerated the remote users logged into a terminal server the script is below. I get the logonid but no account name.  Any help is appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
arrComputers = Array("computername")
For Each strComputer In arrComputers
 
 
Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objwbemLocator.ConnectServer _
    (strComputer, strNamespace, strUser, strPassword)
 
objWMIService.Security_.authenticationLevel = WbemAuthenticationLevelPktPrivacy
 
   
 Set colItems = objWMIService.ExecQuery("Select * from Win32_LogonSession where logontype=10")
 
 If colItems.count = 0 then
 
 WScript.Echo "No Remote Users Logged in"
 
 else
 
 WScript.Echo "Count: " & colItems.count
 
 if MsgBox(" You have " &   colItems.count & " remote users logged in, loop?",4) = vbyes then
 
 
For Each objItem In colItems
 
strLogonID = objItem.LogonID
 
 
  Set colListUser = objWMIService.ExecQuery("Associators of {Win32_LogonSession.LogonId=" & "'" & strLogonID & "'" & "} WHERE AssocClass = Win32_LoggedOnUser Role=Dependent")
      
    For Each objItemUser in colListUser
      WScript.Echo "blah "
       WScript.Echo "User: " & objItemUser.Name
       WScript.Echo "Domain: " & objItemUser.Domain
     
    Next
     
     Wscript.Echo "Session start time: " & objItem.StartTime  & "|" & objItem.Status
     
  
next
 
end if
end if
next
Top Expert: graye
Expert Since: 10/12/2002
Accepted Solutions: 2142
Computer Expertise: Beginner
graye has been an Expert for 6 years 3 months, during which he has posted 6408 comments and answered 2142 questions. graye is just one of 2762 experts in the Microsoft Server Zone.
 
 
20081119-EE-VQP-48 / EE_QW_2_20070628