Monday, March 29, 2010

Who is Logged in now in windows

Here is a WMI script to find out who is logged into the system at the moment.

#whoIsLogged.vbs

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

No comments: