Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to show logged on users? 1

Status
Not open for further replies.

PPettit

IS-IT--Management
Sep 13, 2003
511
US
Is there a way to determine all of the currently logged on users of a Terminal Server? To be more specific, I want to be able to build a list of the users that appear in Terminal Services Manager at the time that I run my script.

The only possibility that I can see at this point is to search through HKEY_USERS for all of the following registry keys:
Code:
HKEY_USERS\[i]<SID>[/i]\Software\Microsoft\Windows\CurrentVersion\Explorer "Logon User Name" [i]username[/i]
or
Code:
HKEY_USERS\[i]<SID>[/i]\Volatile Environment "APPDATA" C:\Documents and Settings\[i]username[/i]\Application Data
From there I would just extract the username data that I need.

Anyone know of a better way to enumerate the currently logged on users?
 
quser from command prompt?

use the .run method and cmd /c quser > c:\user.txt

this will pipe the output to a text file
than parse thru that.

[yinyang] Tranpkp [pc2]
 
Here you go:

Code:
strComputer = "servername"   ' " use "." for local computer 


Set objWMI = GetObject("winmgmts:" _ 
              & "{impersonationLevel=impersonate}!\\" _ 
              & strComputer & "\root\cimv2") 


Set colSessions = objWMI.ExecQuery _ 
    ("Select * from Win32_LogonSession Where LogonType = 10") 


If colSessions.Count = 0 Then 
   Wscript.Echo "No interactive users found" 
Else 
   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 & " FullName: " & objItem.FullName 
     Next 
   Next 
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Thanks Mark. That appears to be just what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top