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?
The script will report the logged on user for the system on which it is run. To
run the script from a command line and see the results displayed in a small
window, type WhoLogon
in the directory whee it is installed. To
run it from the command line and see the results displayed in text form at
the command line, type cscript whologon.vbs
(include the
/nologo
option, if you don't want to see the Windows Host
version number and Microsoft copyright display).
C:\Program Files\Utilities\VBScript>cscript /nologo whologon.vbs
PLUTO\Pam is logged on
In the above example, the user had reported that she couldn't access her favorites in Internet Explorer and that Outlook wasn't working properly. I was able to connect to the system remotely using OpenSSH for Windows to obtain a command line interface. When I ran the WhoLogon script, I could see that she was logged in to the local Pam account on the system, Pluto, rather than into a domain account with the same name, which explained why she didn't see the favorites she normally would see.
Another VBS script to display a logged on computer, which can also be used to display the user logged into a remote system, is LoggedOn.vbs by Cheyenne Harden.
Another alternative for determining who is logged into a system is to use the free PsLoggedOn utility by Mark Russinovich at Sysinternals. This can be downloaded separately or as part of the PsTools package.
There is a whoami utility within the
Native Win32 ports of some GNU utilities, which contains ports of some
common GNU utilities to native Win32. In this context, native means the
executables depend only on the Microsoft C-runtime
References: