Link to home
Start Free TrialLog in
Avatar of Jason Livengood
Jason LivengoodFlag for United States of America

asked on

WMI Query to find logged on Users

I have been playing around with various WMI scripts(.vbs) that query machines on the network to get a list of currently logged on users.  I have had a desent amount of success with this. However the scripts seem to have better luck with workstations(operating systems LIKE XP, Vista, and Windows 7) and not so much luck with server operating systems(specifically Windows Server 2003 sp 2). Attached is a code sample of one of the scripts I have been testing. Notice it returns the username property of objComputer. Seems that the username property  returned by server operating systems is null. I do have proper priviledges to connect to these servers so permissions should not be an issue per say.  I also logged on to the servers to see if the event logs offered any direction on a fix for this but there was nothing that really stood out. Any suggestions as to what the issue could be, or suggestions to tweak the script to make it work with server operating systems would be greatly appreciated.

Jason
Option Explicit 
Dim objWMIService, objComputer, colComputer 
Dim strLogonUser, strLogonUser1, strComputer 

strComputer = "cmdi-ftp" 

strComputer = InputBox("Enter Computer name", _ 
"Find Logon User", strComputer) 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

Set colComputer = objWMIService.ExecQuery _ 
("Select * from Win32_ComputerSystem") 

For Each objComputer in colComputer 
If not objComputer.UserName = "" Then 
strLogonUser = Split(objComputer.UserName,"\") 
strLogonUser(1) = UCase(Left(strLogonUser(1),1))_ 
& Trim(Mid(strLogonUser(1),2,20)) 
Wscript.Echo strLogonUser(1) & " is logged on at " _
& strComputer
Else 
Wscript.Echo "No one is currently logged on at " _
& strComputer
End If 
Next 

' End of Sample Logged on VBScript

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Psy053
Psy053
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jason Livengood

ASKER

OK ... that makes sense actually. Very interesting. Is there a way to have it return those users who are logged on via terminal session as well ?

Jason
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial