?
Solved

WMI Query to find logged on Users

Posted on 2009-12-21
4
Medium Priority
?
3,726 Views
Last Modified: 2013-12-04
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

0
Comment
Question by:jazzcatone
  • 3
4 Comments
 
LVL 14

Accepted Solution

by:
Psy053 earned 2000 total points
ID: 26101691
UserName will not report users logged on via a Terminal Session, only those logged on at the console.
0
 

Author Comment

by:jazzcatone
ID: 26101715
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
0
 
LVL 14

Assisted Solution

by:Psy053
Psy053 earned 2000 total points
ID: 26101737
I haven't found a way to do it using WMI, however, you can use Query.exe from a Server OS to find the information
for example:
Query.exe User /Server:SERVERNAME
0
 
LVL 14

Assisted Solution

by:Psy053
Psy053 earned 2000 total points
ID: 26101752
Just a side thought:
In your VBScript, you can use WMI to determine whether the target computer is a Workstation or Server.
With that information, you'd be able to get the script to determine whether it needs to use the WMI UserName method, or a Shell.Run Query.exe method.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Security measures require Windows be logged in using Standard User login (not Administrator).  Yet, sometimes an application has to be run “As Administrator” from a Standard User login.  This paper describes how to create a shortcut icon to launch a…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question