WMI Tutorial Who is Logged on at That Computer?

WMI Tutorial – Who is Logged on at that Computer?

In many ways, this simple script is my equivalent of the normal ‘Hello World’ example that most scripts writers choose for their introduction.  This VBScript echoes to screen the name of the user who is logged on at the computer.  I choose this starting point to run the WMI commands through their paces, because the prerequisites are minimal and the chances of success are high.

Topics for Who is Logged on at that Computer

 ♦

Example 1 – Discover who is Logged on at a Computer

Let us suppose that you want to know which user is logged on at a particular machine.  We will use this simple example as a vehicle to test drive WMI commands. There is a practical twist in the tail, if you change the value of strComputer then you can discover who is logged on at any machine on your network.  Incidentally, it surprised me how often I needed to know this or similar user information for more advanced scripts.

Prerequisites

If you wish to test the username for the local machine, then you can logon as an ordinary account.  However if you want to find out who is logged on at another machine, I recommend that you logon as administrator.  This is a script will execute equally well on a Windows server or an XP machine.

Instructions for Creating your WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: WhoLogon.vbs 
  3. Double click WhoLogon.vbs and check the username.

Script to Discover Who is Logged on at "UserMachine"

‘ WhoLogon.vbs
‘ Sample VBScript to discover which user is logged on
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2 – December 2010
‘ ———————————————–‘
Option Explicit
Dim objWMIService, objComputer, colComputer, strComputer

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer
Wscript.Echo objComputer.UserName & " is logged on"
Next

‘ End of Sample Script. Who is logged on?

Recommended: Solarwinds’ Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Analyzer because it enables me to see WHO has permissions to do WHAT at a glance.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, and takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free SolarWinds utility saves when you are troubleshooting authorization problems for user’s access to a resource.  Give this permissions monitor a try – it’s free!

Download SolarWinds’ Free Permissions Analyser – Active Directory Tool

WMI Tutorial Learning Points

From a WMI perspective

1) Let us see how to connect to the Root of the CIM namespace.   Here is the standard command which opens up an imaginary pipe to all the WMI objects and their properties:
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2").

2) In this instance we need security clearance to discover who is logged on at the other machine, this is why we add :
 & "{impersonationLevel=impersonate}!\\" _ .

3) Set colComputer = objWMIService.ExecQuery _ is a standard WMI phrase that prepares VBScript to execute a process.  In this example, we want to execute the WQL command:  Select * from Win32_ComputerSystem.  The part that we are particularly interested in is _ComputerSystem.  Win32 has dozens of scriptable process, here we need to query not the disk or processor, but he ComputerSystem component.

From a VBScript perspective

4) Even though there is only one user, the script needs the loop: For Each….In… Next.  Believe me, I tried to simplify the script by removing the loop, but all I got was an object required error.

5) .UserName is a property of objComputer.  It happens that UserName is the property that we seek, however, we could have substituted or added more properties, for example .NumberOfProcessors or .SystemType

6) VBScript does not understand word-wrap, so if the command spans two lines we add the _ (Underscore) at the end of the first line.

7) To get a quick success I set strComputer to "." (meaning the local machine).  Why not change the value of strComputer to another machine.  For example strComputer = "AnotherMachine".  Carefully make this change, then run the script again.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Example 2 – Advanced WMI Script – Who is Logged on?

This script builds on Example 1 and adds an input box, which gives you the flexibility to choose any machine on the network.  It also adds cosmetic VBScript commands to isolate the username and present it in proper case.

‘ WhoLogonInput.vbs
‘ Sample VBScript to discover which user is logged on
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.4 – December 2010
‘ ——————————————————-‘
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer

strComputer = "."
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
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

Next

‘ End of Sample Logged on VBScript

Note: See more on using WMI Monitor.

WMI Tutorial Learning Points

1) I admit that this second example is more about pure VBScript than WMI.  The hidden message is that the WMI commands need a VBScript wrapper to display their output.

2) The advantage of studying InputBox in this simple script, is that you can master its syntax here then apply it to my other more complex WMI / VBScript examples.  InputBox has three parameters, only the first item is compulsory.  That first parameter is a message which tells the user what to place in the box.  The second parameter is the title of the message box, while the third parameter is the default value if the user just clicks the OK button, "." in my example.

3) Once again I have introduced the classic scripting commands, UCase, Trim and Mid to tidy up the output.

4) In Windows scripting, the output is often a long string whereas you only want one element.  In this example, I wish to separate the username from the domain\username.  For this job, I have introduced the VBScript command Split, and told it that the backsplash is the delimiter.

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive software, download a free trial of SAM (Server & Application Monitor)

Example 3 – Error-correcting Code Sent In by John Eck

John kindly wrote in to point out that the above script fails if you choose a machine where no one is logged on.  Not only did John alert me to the error, but also he kindly provided the error-correcting code.

‘ WhoLogonInput.vbs
‘ WMI Tutorial to discover which user is logged on
‘ Author Guy Thomas and John Eck
‘ Version 2.5 – December 2010
‘ ——————————————————-‘
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strLogonUser1, strComputer

strComputer = "."
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

WMI Tutorial Learning Point

Note: The key extra element is: If not objComputer.UserName = "" Then …
I like John’s extra code because it not only solves the problem of a machine with no one logged on but also demonstrates the use of ‘not’.

Summary – Who is Logged on WMI Script

This is a great way to get started with WMI.  Have some fun by checking who is logged on at a computer.  Then follow up by studying the WMI command which connect to the CIM namespace and query the ComputerSystem object.

See more VBScript service examples:

VBScript Listing Process   • VBscript Starting a Process   VBScript Restart Service

• VBscript Kill Process   • WMI Tutorial   • Win32_Process   • PowerShell Services

WMI Home   • WMI Win32   • VBScript Echo   • WMI VBScript  • Free WMI Monitor