WMI Query to find logged on Users

Posted on 2009-12-21
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.

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


Wscript.Echo "No one is currently logged on at " _

& strComputer

End If 


' End of Sample Logged on VBScript

Open in new window

Question by:jazzcatone
    LVL 14

    Accepted Solution

    UserName will not report users logged on via a Terminal Session, only those logged on at the console.

    Author Comment

    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 ?

    LVL 14

    Assisted Solution

    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
    LVL 14

    Assisted Solution

    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.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This is a short article about OS X KeRanger, and what people can do to get rid of it.
    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 the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now