Share via


Who Is Logged In?

A few months back one of my customers was trying use RDP to connect to a large number of desktop computers to manually install some security updates.  If the computer was being  used the admin had to wait until the person using the computer logged out and find another computer.  Unfortunately there was no easy way to tell if a person is logged in until you start a remote desktop session, enter your credentials and try to connect.  If someone is logged on locally you will be prompted to log them off or disconnect.  It was taking quite a while to find unused computers to work on.  

I created the script below to check a computer and see if anyone is logged into it before trying to connect via RDP.  The script uses WMI to connect to the remote computer and is much faster than trying to connect each time.

Copy the code below and paste into Notepad.  Save as LOGON_STATUS.VBS.  The script must be run with Administrator credentials to connect to the remote computers.  The script runs a loop and will stay running until you click the CANCEL button to exit the script.

'<<<<<<<BEGIN SCRIPT>>>>>>>>>  

'**********************************************
'  SCRIPT: LOGON_STATUS.VBS
'  AUTHOR:
'  DATE: 07/22/08
'  VERSION: 1.2
' PURPOSE: Used to determine if a user is logged into a remote computer
'                    Run script as an administrator
'   USAGE:
'         
' REVISION: 00/00/00 - change
'    08/12/08 - added loop logic and QUIT logic
'    11/23/09 - Improved error handling
'**********************************************
ON ERROR RESUME NEXT

DO WHILE UCASE(strComputer) <> "QUIT"

strCOmputer= InputBox("Enter Remote Computer Name.  Click CANCEL to exit script","Computer Name")

 

   Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 'wscript.echo err.number 'DEBUG

 IF ERR.NUMBER = -2147217375 THEN
   'wscript.echo "quit"'DEBUG
   WSCRIPT.QUIT

 END IF

 

 IF err.number = 462 THEN
     Wscript.echo "Could Not Contact Computer"
 END IF

 IF err.number = 70 THEN
     Wscript.echo "There was a problem accessing WMI on remote computer"
 END IF
 
 IF ERR.NUMBER = 0 THEN
     Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
       For Each objComputer in colComputer
          Wscript.Echo "Logged-on user: " & objComputer.UserName
       Next
   ELSE
    Wscript.echo "Error Code Returned from remote computer is: " & err.number
 END IF

err.clear

LOOP

'<<<<<<< END SCRIPT>>>>>>>>>

Comments

  • Anonymous
    September 28, 2009
    The comment has been removed