Obscurum per Obscurius

Explaining the obscure by means of the more obscure

Who Is Logged In?

Who Is Logged In?

  • Comments 1

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>>>>>>>>>

 

Leave a Comment
  • Please add 5 and 4 and type the answer here:
  • Post
  • I had a similar problem, but I found that you can query a remote machine's running tasks and task owners.  The explorer.exe process only runs when a user is logged in, so if you use "tasklist" to query a remote machine, you'll get the user logged in (if there is one).

    tasklist /s machinename /fi "imagename eq explorer.exe" /v

    I made a script that queries my AD structure for machines, does a quick edit of the machinelist, then pipes that into a FOR command...  You do need the 'gsar' utility to do the edit commands.  I've double spaced between lines, so the dsquery and the for lines are each one very long line.

    ---start batch---

    del /q loggedin.txt

    dsquery computer "OU=(yourOU),DC=domain,DC=com" -name * -limit 0 | dsget computer -samid > workstationlist.txt

    gsar -ssamid -r -o workstationlist.txt

    gsar -s$ -r -o workstationlist.txt

    gsar -s "dsget succeeded" -r -o workstationlist.txt

    for /f "skip=1 delims= " %%i in (workstations.txt) do for /f "skip=3 usebackq tokens=7 delims= " %%j in (`tasklist /s %%i /fi "imagename eq explorer.exe" /v`) do echo %%i %%j >> loggedin.txt

    sort loggedin.txt /o loggedin.txt

    ---end batch---

Page 1 of 1 (1 items)