Jump to content

Query and sign off disconnected users (Win10)


therks
 Share

Recommended Posts

So I have no example code currently because I'm not sure where to even start.

We're using the parental controls on the computer and the kids each have their own user profiles with limited time. The problem we're encountering is when their time runs out, or if they just hit Win+L and leave the computer, their games are still running in the background. I was hoping to write up a quick AutoIt script and throw it in a recurring scheduled task to just sign off any of the kid's profiles that's not currently active. I found some suggestions to use "query session" and then "logoff [user]" in the command line (which it seems I could easily automate with AutoIt) but that's only available on Win Pro and this system is running Home (and I even tried copying the query.exe from a Pro system but it errors out). Is there some AutoIt equivalent to the query and logoff functions or am I stuck?

Edit: I'm adding some clarification to exactly what I'm hoping to get out of this script.

I'd like to setup a scheduled task that runs outside of the current user (perhaps under the SYSTEM account?) in case NO user is currently active, that will detect which users are currently signed in. If users from a predefined list are not currently active, sign them off entirely.

So let me give two examples:

Example #1:

- I am logged in, but not active (I left my browser open and locked the computer)
- Nephew A is logged in, but not active (his time ran out while his game was running and the computer auto-locked)
- Nephew B is logged in, and currently playing a game

When script runs, ignore my profile (leave it signed in), log out Nephew A (closing his game), and ignore Nephew B because he's actively using the computer.

Example #2:

- I am logged in
- Nephew A is logged in
- Nephew B is logged in
- No users are currently active (Windows is sitting on the choose user / sign in screen)

When script runs, ignore my profile, log out Nephew A and Nephew B.

 

I would perceive pseudo code, going as such:

$aChildUsers = [ 'NephewA', 'NephewB' ]

$aUserList = GetSignedInUsers()

For $i = 0 to UBound($aUserList)-1
    If Not UserIsActive($aUserList[$i]) Then
        For $j = 0 to UBound($aChildUsers)-1
            If $aUserList[$i] = $aChildUsers[$j] Then LogOffUser($aUserList[$i])
        Next
    EndIf
Next

Obviously, GetSignedInUsers(), UserIsActive(), LogOffUser() are not real functions. What I'm searching for is something of their equivalent.

Thanks for your time!

Edited by therks
Added clarification
Link to comment
Share on other sites

@therks
Didn't have spare time to build an example, but you can definitely have a look at PsLoggedOn from SysInternals suite.
Just execute the command, get the result, and use the logoff command with the specified users.
Hope it helps.

Cheers ^_^

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

I am assuming that there is only one user for that computer at a time.  Here how I would do it :

1- Get list of users with WMI Win32_LoggedOnUser

2- Get _Timer_GetIdleTime()

3- if Idle Time > xy minutes and User is a child then ShutDown ($SD_REBOOT+$SD_FORCE)

If that makes sense and need help to convert this pseudo-code into real code, let me know...

Link to comment
Share on other sites

10 hours ago, FrancescoDiMuro said:

@therks
Didn't have spare time to build an example, but you can definitely have a look at PsLoggedOn from SysInternals suite.
Just execute the command, get the result, and use the logoff command with the specified users.
Hope it helps.

Cheers ^_^

Well PsLoggedOn is promising. However I'm not sure how to tell who the currently active user is (assuming I can get this to run from the SYSTEM account or somesuch), or how to log off other users (the logoff command doesn't seem to be available on Home either).

8 hours ago, Nine said:

I am assuming that there is only one user for that computer at a time.  Here how I would do it :

1- Get list of users with WMI Win32_LoggedOnUser

2- Get _Timer_GetIdleTime()

3- if Idle Time > xy minutes and User is a child then ShutDown ($SD_REBOOT+$SD_FORCE)

If that makes sense and need help to convert this pseudo-code into real code, let me know...

Well there's only ever one active user, but often my brother or I will be logged on but disconnected. For example. In this example, I only want to sign out the user at the top.

So I'm having some issues with your implementation:

  • Win32_LoggedOnUser seems to list every user, logged on or not.
  • How would I get the idle time (_Timer_GetIdleTime) for each user?
  • How do I use Shutdown on another user? (Also wouldn't I just want $SD_LOGOFF+$SD_FORCE, because I don't want to restart the whole computer).

Thanks for the input so far though guys. 

I'm going to edit my top post to clarify what I'm looking for.

Edited by therks
Link to comment
Share on other sites

I see.  Everybody leaves the computer but stay on. Maybe a change of habit would be recommended.  If you want to supervise your children,  my approach is flawless.  But if you want to preserve your habits, good luck then.

Link to comment
Share on other sites

1 hour ago, Nine said:

I see.  Everybody leaves the computer but stay on. Maybe a change of habit would be recommended.  If you want to supervise your children,  my approach is flawless.  But if you want to preserve your habits, good luck then.

The issue with just shutting off the computer is sometimes we access it from other computers when we are not at home.

Link to comment
Share on other sites

Well I figured out a method. Not quite the way I intended, but isn't that usually the way of AutoIt? 😁

So, I created this little script:

If $CmdLine[0] Then
   For $i = 1 to $CmdLine[0]
      If @UserName = $CmdLine[$i] Then
         If Not Shutdown(4) Then MsgBox(0x10, @ScriptName & ' - Error', 'There was an error (#' & @error & ') trying to log off this user: ' & @UserName)
      EndIf
   Next
Else
   MsgBox(0x40, @ScriptName, 'Will force log off if current user matches any name given as command parameters' &@CRLF&@CRLF& 'ie:    ' & @ScriptName & ' "Aaron" "Bob Smith"' &@CRLF&@CRLF& 'Will log off Aaron or Bob Smith')
EndIf

And then I created a Scheduled Task triggered to run "On workstation lock of any user", gave it parameters of the kids' usernames, and Bob's your uncle.

In my brief testing it seems to work alright.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...