Jump to content
Sign in to follow this  

Issues with Shutdown Function

Recommended Posts


Hi everyone. I work for a university, and as a part of our IT security policies we must have a solution to log off idle users after a certain period of time. I've used a script to monitor idle time and log off idle users for several years without any issues, until we imaged our labs with Windows 10 (1703). Now, when issuing the shutdown command to log off, it gets stuck logging off if the user has open and unsaved documents, requesting to save them before logging off, even if utilizing the force and force if hung options. Back in Windows 7, just utilizing Shutdown(16) was adequate, but I've also tried Shutdown(20) with the same results. Other than killing open processes before calling shutdown, does anyone have any ideas that may help?

What's weird is I can run the same script with open/unsaved documents on my computer and it logs me off without any issues, so I'm not entirely sure what's triggering the problem on our lab computers.


#include <Misc.au3>
AutoItSetOption("TrayIconHide", 1)

;Check if the script is already running
If Not _Singleton(@ScriptName, 1) Then Exit

While 1
   $iIdleTime = _Timer_GetIdleTime()
   ;Time is in ms, so 1000 is 1 second
   If $iIdleTime >= 900000 Then
      ;Log off
      $sIdleCheck = MsgBox(48, "Idle Session", "University IT security policies dictate that any publicly available computer must log off idle users after a certain period of time." & @CRLF & @CRLF & "The computer has been idle for 10 minutes. If you are still at the computer, please press OK to reset the timer. Otherwise, you will be logged off in 90 seconds.", 90)
      If $sIdleCheck <> 1 Then
   ;Only check idle time every 5 seconds to prevent excessive CPU/memory usage

; #FUNCTION#;===============================================================================
; Name...........: _Timer_GetIdleTime()
; Description ...: Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; Syntax.........: _Timer_GetIdleTime()
; Parameters ....: None
; Return values .: Success - integer ticks since last (approx. milliseconds) since last activity
;                 Failure - Sets @extended = 1 if rollover occurs (see remarks)
; Author ........: PsaltyDS at http://www.autoitscript.com/forum
; Modified.......: v1.0.0  --  03/20/2008 First version
;                 v1.0.1  --  06/11/2008 Fixed bug when idle time = 0ms
; Remarks .......: The current ticks since last system restart will roll over to 0 every 50 days or so,
;                 which makes it possible for last user activity to be before the rollover, but run time
;                 of this function to be after the rollover.  If this happens, @extended = 1 and the
;                 returned value is ticks since rollover occured.
; Related .......: _CheckIdle()
; Link ..........;
; Example .......; Yes
Func _Timer_GetIdleTime()
; Get ticks at last activity
    Local $tStruct = DllStructCreate("uint;dword");
    DllStructSetData($tStruct, 1, DllStructGetSize($tStruct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($tStruct))

; Get current ticks since last restart
    Local $avTicks = DllCall("Kernel32.dll", "int", "GetTickCount")

; Return time since last activity, in ticks (approx milliseconds)
    Local $iDiff = $avTicks[0] - DllStructGetData($tStruct, 2)
    If $iDiff >= 0 Then
    ; Normal return
        Return $iDiff
    ; Rollover of ticks counter has occured
        Return SetError(0, 1, $avTicks[0])
EndFunc  ;==>_Timer_GetIdleTime


Share this post

Link to post
Share on other sites

Have you tried the following combination?



You can also close all windows  or kill all processes right before logging off.

Edited by Au3Builder

Share this post

Link to post
Share on other sites

Actually, it looks like Windows Defender SmartScreen started blocking the compiled script on some of the computers. I'm looking into a way to add it as an exception. Thanks for the suggestion!

Edited by chaoticyeshua

Share this post

Link to post
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
Sign in to follow this