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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get logged-on usernames on Windows 2003

Status
Not open for further replies.

inselaffe

Programmer
Oct 4, 2007
1
GB
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?
 
What if you tried getting the info depending on the instances of explorer.exe??

i.e.

Code:
Option Explicit

Dim strComputer : strComputer = "."
Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Dim colProcesses : Set colProcesses = objWMIService.ExecQuery(_
				   "Select * From Win32_Process Where Name = 'explorer.exe'")
Dim objProcess, strUserName, strDomainName
For Each objProcess In colProcesses
	objProcess.GetOwner strUserName, strDomainName
	WScript.Echo strDomainName & "\" & strUserName
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top