Introduction to 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
♦
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.
PrerequisitesIf 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
- Copy and paste the example script below into notepad or a VBScript editor.
- Save the file with a .vbs extension, for example: WhoLogon.vbs
- 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 http://computerperformance.co.uk/ ' Version 1.2 - December 2005 '
--------------------------------------------------------------' 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?
Guy Recommends: SolarWinds LANSurveyor
LANSurveyor will produce a neat diagram of your network topology. But that's
just the start;
LANSurveyor can
create an inventory of the hardware and software
of your machines and network devices. Other neat features include dynamic
update for when you add new devices to your network. I also love the ability to export
the diagrams
to Microsoft Visio.
Finally, Guy bets that if you take a free trial of LANSurveyor then you will
find a device on your network that you had forgotten about, or someone else
installed without you realizing!
Download a Free Trial of LANSurveyor
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.
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 http://computerperformance.co.uk/ ' Version 2.4 -
December 2005 ' -------------------------------------------------------' 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
WMI Tutorial Learning Points1) 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. 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 ' Sample VBScript to discover which user is logged on ' Author Guy Thomas and John Eck ' Version 2.5 - December 2005 '
-------------------------------------------------------' 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
Learning Points1) 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'.
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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
See Also
● WMI Home ● WMI Basics ● WMI
Secrets ● Terminate a Windows Process |