Jump to content

Shutting down a computer when no one is logged on


Recommended Posts

Hi everyone.

I was given the challenge of coming up with a way of automatically shutting down computers that are not being used by anyone. I created a "proxy" screensaver that when activated, checks the username calling it. If the username is SYSTEM it tries to shutdown the computer as it means no one is logged on. Otherwise, it simply runs the "real" screensaver.

I made my script behave like a screensaver, by making it adhere to the specifications found in: http://support.microsoft.com/kb/182383

This is all working as I expected, except for the fact that the computers are not shutting down. Little detail, huh? :) It seems that AutoIT's Shutdown function only works when someone is logged on. (?)

This is the code I'm using

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=SSaver.ico
#AutoIt3Wrapper_Outfile=NJGreen.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Comment=NJGreen
#AutoIt3Wrapper_Res_Description=NJGreen
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Carlos Alcobia
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.3.0.0
    Author:         Carlos Alcobia
    
    Script Function:
    Shuts down computers not being used
#ce ----------------------------------------------------------------------------

Local $RealScreenSaver = "C:\Windows\system32\RealScreenSaver.scr" ;The full pathname for the real screensaver
Local $TimeToWait = "300000" ;This needs to be specified in milliseconds

Select
    Case $CmdLine[0] <= 0
        _Settings()
    Case $CmdLine[0] = 1
        _ParseArguments($CmdLine[1])
    Case $CmdLine[0] = 2 And ($CmdLine[1] = "/p" Or $CmdLine[1] = "/P")
        _Preview($CmdLine[2])
    Case $CmdLine[0] = 2 And (Not $CmdLine[1] = "/p" Or Not $CmdLine[1] = "/P")
        _NoValidSwitches()
    Case $CmdLine[0] > 2
        _NoValidSwitches()
EndSelect


Func _NoValidSwitches()
    MsgBox(64, "NJGreen", "Invalid switch provided." & @CRLF & @CRLF & "Use:" & @CRLF & "/C or nothing  to show the Settings dialog" & @CRLF & "/P <HWND>   to preview Screen Saver as child of window <HWND>" & @CRLF & "/S        to run the Screen Saver")
    Exit
EndFunc   ;==>_NoValidSwitches

Func _Settings()
    MsgBox(0, "NJGreen Settings", "NJGreen Screen Saver" & @CRLF & @CRLF & "By Carlos Alcobia for XXXXXXXXXXXXX." & @CRLF & @CRLF & "This screen saver has no options that you can set.")
    Exit
EndFunc   ;==>_Settings

Func _Preview($HWND)
    Exit
EndFunc   ;==>_Preview

Func _ParseArguments($Arg)
    $UpArg = StringUpper($Arg)
    $Arg = StringLeft($UpArg, 2)
    If $Arg = "/C" Then _Settings()
    If $Arg = "/S" Then _GreenDecision()
    _NoValidSwitches()
EndFunc   ;==>_ParseArguments

Func _GreenDecision()
    If Not @OSVersion = "WIN_XP" Or Not @OSVersion = "WIN_VISTA" Then _ScreenSave()
    If StringUpper(@UserName) = "SYSTEM" Then _GoGreen()
    _ScreenSave()
    Exit
EndFunc   ;==>_GreenDecision

Func _ScreenSave()
    RunWait($RealScreenSaver & " /s")
    Exit
EndFunc   ;==>_ScreenSave

Func _GoGreen()
    Local $PID = Run($RealScreenSaver & " /s")
    Local $begin = TimerInit()
    While ProcessExists($PID)
        $dif = TimerDiff($begin)
        If $dif >= $TimeToWait Then Shutdown(5)
    WEnd
    Exit
EndFunc   ;==>_GoGreen

This is then compiled and the extension changed to .SCR

I've established that If I change the code so that it tries to shutdown the computer even when there is a user logged on, it does work and the computer shuts down, but can't get it to work when no one is logged on.

Do you have any suggestions? Thanks.

Edited by zodiac
Link to comment
Share on other sites

I had to replace this:

If $dif >= $TimeToWait Then Shutdown(5)oÝ÷ Û­Øb±«­¢+ÙIÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíÍ¡Ñͭ̽±Ñ½½Ñ¸É¹M¡ÕѽݸÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤)IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíÍ¡Ñͭ̽ÉѽITMeMQ4½Í½¹½Ñ¸É¹M¡Õѽݸ½ÑÈÅÕ½ÐíÍ¡ÕѽݸµÌµµÐÀÀÅÕ½Ðì½MPÌäìµÀì!=UHµÀìÅÕ½ÐìèÅÕ½ÐìµÀì5%8µÀìÅÕ½ÐìèÀÀÅÕ½Ðì°ÅÕ½ÐìÅÕ½Ðì°M]}!%¤)IÕ¹]¥Ð¡
½µMÁµÀìÅÕ½Ðì½ÅÕ½ÐìµÀìÌäíÍ¡Ñͭ̽ÉÕ¸½Ñ¸É¹M¡ÕѽݸÌäì°ÅÕ½ÐìÅÕ½Ðì°M]}!%

And now it works. It seems that a screensaver has a somewhat limited set of permissions, but that's just a theory.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...