Jump to content
Sign in to follow this  
shaunex

Time restrcited shutdown

Recommended Posts

shaunex

Hi

I am trying to script an exe that will check the time and if it is between a certain time period will run the shutdown.exe which can also be cancelled by the user but I am having several issues that I hope somebody can help with.

CODE

If @HOUR > 10 Or @HOUR < 14 Then

ShellExecute ("shutdown", "-s" , "-t 90")

$cancel=MsgBox(64, "Shutdown", "LANDesk is shutting down your PC! Press OK to cancel the shutdown")

if $cancel=1 then ShellExecute ("shutdown", "-a")

EndIf

If I change the OR to AND (which is what I need)it does not run at all, I need the program only to run between certain hours of the day (the current times I put in for testing, so it should be 20:00 and 3:00). The second issue is that the shellexecute is picking up the -s switch but not the -t, so just gives a default shutdown of 30 seconds and I would like this to be longer. The msgbox is also causing me a headache as because the location is in the centre of the screen the windows shutdown screen covers it up.

I have been trying to keep the executable as small as possible as this will be pushed out to a large number of remote clients.

Share this post


Link to post
Share on other sites
d4rk

the second issue, why don't use shutdown() for simple ?

the first issue , with AND , i'm sure it can't work


[quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys

Share this post


Link to post
Share on other sites
shaunex

the second issue, why don't use shutdown() for simple ?

the first issue , with AND , i'm sure it can't work

I was looking into using this but there doesn't seem to be a time variable for how long the shutdown(1) would take. I would like to be able to give the users around 1.30 to 2 minutes grace.

I thought the AND wouldn't work but can not figure out why. As long as there was a gap in the time period the logic should work.

Share this post


Link to post
Share on other sites
monoceres

Why don't you use

Sleep(90*1000)
Shutdown(1)oÝ÷ ØêÚºÚ"µÍÌÍÝ[YU[Y[]

BÂÛY
L
B[[[YY  ÌÍÝ[YIÝÊL
L

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
DarkMatter

You could try something like the following:

#include <GUIConstants.au3>
AdlibEnable("CheckButton", 1)

$Form1 = GUICreate("System Shutdown", 432, 83, 193, 115)
$Label1 = GUICtrlCreateLabel("Your system will shutdown in 0 Seconds. Press the cancel button to halt the shutdown.", 8, 8, 412, 17)
$Button1 = GUICtrlCreateButton("&Cancel", 0, 40, 75, 25, 0)
GUISetState(@SW_SHOW)

ShutdownComputer(10)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            Exit
    EndSwitch
WEnd

Func ShutdownComputer($Time)
    Local $i
    
    For $i = $Time To 0 Step - 1
        GUICtrlSetData($Label1, "Your system will shutdown in " & $i & " Seconds. Press the cancel button to halt the shutdown.")
        Sleep(1000)
    Next
    Shutdown(4)
EndFunc

Func CheckButton()
    If GUIGetMsg() = $Button1 Then Exit
EndFunc

Just create a GUI and have the user click cancel if it is before the timeout. This is a VERY ruff script, but it should work.

Edited by DarkMatter

[sub]Quantum mechanics: The dreams stuff is made of[/sub]

Share this post


Link to post
Share on other sites
shaunex

Why don't you use

Sleep(90*1000)
Shutdown(1)oÝ÷ ØêÚºÚ"µÍÌÍÝ[YU[Y[]

BÂÛY
L
B[[[YY  ÌÍÝ[YIÝÊL
LoÝ÷ Ûú®¢×­æ­ß}÷
§çb¶Ú-+ºÚ"µÍYÕT    ÈM[IÌÍØØ[Ù[SÙÐÞ
    ][ÝÔÚ]ÝÛ][ÝË ][ÝÓSÚÈÈÚ][ÈÝÛ[ÝÉÌÌÎÈÜÈÒÈÈØ[Ù[]ÝÛ][ÝÊBZY   ÌÍØØ[Ù[LH[^]TÛY
L
L
BTÚ]ÝÛJB[Y

and is working fine, just need sort out the time issue.

You have been great help and sorted it out really quickly.

Share this post


Link to post
Share on other sites
Monamo

Fixed your parameter entry for ShellExecute() to use the 90 second timer. And the nested If statement should take care of your "window" of applicable hours:

CODE

#include <GUIConstants.au3>

#NoTrayIcon

While 1

If @HOUR > 10 Then

If @HOUR < 14 Then

ShellExecute("shutdown", "-s -t 90")

$hGUIPrompt = GUICreate("Shutdown", 300, 150, 10, 10, $DS_MODALFRAME)

GUICtrlCreateLabel("LANDesk is shutting down your PC! Press 'Cancel' to cancel the shutdown.", 20, 10, 260, 40)

$buttonCancel = GUICtrlCreateButton("Cancel", 100, 100, 100, 20)

GUISetState()

ExitLoop

EndIf

EndIf

WEnd

While 1

$msg = GUIGetMsg()

Select

Case $msg = $buttonCancel

ShellExecute("shutdown", "-a")

Exit

EndSelect

WEnd


- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
shaunex

Fixed your parameter entry for ShellExecute() to use the 90 second timer. And the nested If statement should take care of your "window" of applicable hours:

CODE

#include <GUIConstants.au3>

#NoTrayIcon

While 1

If @HOUR > 10 Then

If @HOUR < 14 Then

ShellExecute("shutdown", "-s -t 90")

$hGUIPrompt = GUICreate("Shutdown", 300, 150, 10, 10, $DS_MODALFRAME)

GUICtrlCreateLabel("LANDesk is shutting down your PC! Press 'Cancel' to cancel the shutdown.", 20, 10, 260, 40)

$buttonCancel = GUICtrlCreateButton("Cancel", 100, 100, 100, 20)

GUISetState()

ExitLoop

EndIf

EndIf

WEnd

While 1

$msg = GUIGetMsg()

Select

Case $msg = $buttonCancel

ShellExecute("shutdown", "-a")

Exit

EndSelect

WEnd

This is looking pretty good so far, exactly what I need. I was looking at far to simply I feel and not utilising what Autoit has, more scripting practise needed I think. Thanks very much.

Share this post


Link to post
Share on other sites
Squirrely1

Another squirrely method:

; A script to monitor the time, and begin shutdown based upon certain criteria.

;#NoTrayIcon; Disallow exiting the process.

$BeginEffectivePeriod = 20; after 8 PM
$EndEffectivePeriod = 3; before 3 AM

While 1
    Sleep(300000); check the time only once every 5 minutes
    CheckTheTime()
WEnd

Func CheckTheTime()
    $hour = Number(@HOUR)
    If $hour > $BeginEffectivePeriod Or $hour < $EndEffectivePeriod Then
    ;WinSetState("Program Manager", "", @SW_HIDE); Hide icons and taskbar
    ;Sleep(60)
        WinClose("Program Manager"); bring up the Shutdown GUI
        Sleep(90000); wait 90 seconds for a Cancel
        Send("u"); execute shutdown
    ;WinSetState("Program Manager", "", @SW_SHOW)
    EndIf
EndFunc  ;==>CheckTheTime

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
shaunex

Fixed your parameter entry for ShellExecute() to use the 90 second timer. And the nested If statement should take care of your "window" of applicable hours:

CODE

#include <GUIConstants.au3>

#NoTrayIcon

While 1

If @HOUR > 10 Then

If @HOUR < 14 Then

ShellExecute("shutdown", "-s -t 90")

$hGUIPrompt = GUICreate("Shutdown", 300, 150, 10, 10, $DS_MODALFRAME)

GUICtrlCreateLabel("LANDesk is shutting down your PC! Press 'Cancel' to cancel the shutdown.", 20, 10, 260, 40)

$buttonCancel = GUICtrlCreateButton("Cancel", 100, 100, 100, 20)

GUISetState()

ExitLoop

EndIf

EndIf

WEnd

While 1

$msg = GUIGetMsg()

Select

Case $msg = $buttonCancel

ShellExecute("shutdown", "-a")

Exit

EndSelect

WEnd

Ok I have been testing this but the exe just runs in the loop until the time is reached. I have tried removing the loop but get an error that the $msg variable hasn't been declared.

Share this post


Link to post
Share on other sites
Squirrely1

Try this TESTED code:

; Script that attempts shutdown if computer is still on after 8pm
;#NoTrayIcon
#include <GUIConstants.au3>
Global $msg
Global $hGUIPrompt
Global $buttonCancel
Global $BeginEffectivePeriod = 20; after 8 PM
Global $EndEffectivePeriod = 3; before 3 AM
While 1
    $hour = Number(@HOUR)
;If $hour > $BeginEffectivePeriod Or $hour < $EndEffectivePeriod Then
; if its later than 8pm and/or earlier than 3 am then ...
        Build_Controls()
        While 1
            $msg = GUIGetMsg()
            Sleep(60)
            If $msg = $buttonCancel Then
                Delete_Controls()
                ExitLoop 1
            EndIf
        WEnd
;EndIf
    Sleep(300000); checking the time only every five minutes
WEnd

Func Build_Controls()
    $hGUIPrompt = GUICreate("Shutdown", 300, 150, 10, 10, $DS_MODALFRAME)
    GUICtrlCreateLabel("LANDesk is shutting down your PC! Press 'Cancel' to cancel the shutdown.", 20, 10, 260, 40)
    $buttonCancel = GUICtrlCreateButton("Cancel", 100, 100, 100, 20)
    Run(@ComSpec & " /c shutdown -s -t 90", "", @SW_HIDE)
    Sleep(300)
    GUISetState()
EndFunc ;==>Build_Controls

Func Delete_Controls()
    Run(@ComSpec & " /c shutdown -a", "", @SW_HIDE)
    Sleep(300)
    GUIDelete($hGUIPrompt)
EndFunc ;==>DeleteControls

Edit: Code reworked.

Un-rem these lines after you have tested the code:

Line2: ;#NoTrayIcon

Line11: ;If $hour > $BeginEffectivePeriod Or $hour < $EndEffectivePeriod Then

Line22: ;EndIf

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
arminius

I ran this code, the script never stopped trying to shut down the comp no matter what the time was

Share this post


Link to post
Share on other sites
Squirrely1

I ran this code, the script never stopped trying to shut down the comp no matter what the time was

If you are referring to my code - arminius, guy with Latin-seeming name -then note please, that at the end of my code is a little note indicating that you should un-REM some lines in the code, including these:

Line11: ;If $hour > $BeginEffectivePeriod Or $hour < $EndEffectivePeriod Then

Line22: ;EndIf

These lines were REM'd-out, or commented-out in order that another part of the code that the OP was having a problem with, could be tested, without waiting for the target hours 8pm to 3am. Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
shaunex

If you are referring to my code - arminius, guy with Latin-seeming name -then note please, that at the end of my code is a little note indicating that you should un-REM some lines in the code, including these:

These lines were REM'd-out, or commented-out in order that another part of the code that the OP was having a problem with, could be tested, without waiting for the target hours 8pm to 3am.

Hi Squirrely1

tried this one with the lines REM'ed out but it is still running in a loop waiting for the time to come around.

Share this post


Link to post
Share on other sites
Squirrely1

Hi Squirrely1

tried this one with the lines REM'ed out but it is still running in a loop waiting for the time to come around.

Hi shaunex

The code I posted is not something I would have written for general distribution, but was instead intended as a corrected version of code that you - the apparent OP - wrote; I tried to change as little as possible.

Have you tried adjusting the Sleep function parameter in the loop?

I tried the code again on my machine and it immediately brings up the shutdown routine. I am running AutoIt version 3.2.10.0 on Windows XP Home Edition Service Pack 1.

Let me recommend that you do a Search in the AutoIt help hile for function called Shutdown, which is a better choice since the legacy executable called shutdown.exe may not work in some versions of Windows. You can accomplish almost the same thing your script does using the AutoIt functions, instead of calling the .exe this way:

Run(@ComSpec & " /c shutdown -s -t 90", "", @SW_HIDE)

This code will not throw an error if your machine's version of shutdown.exe does not exist or work right.

When you log on to the machine you are using, do you have administrator privileges or permissions sufficient to shutdown that particular machine?

My posted version of your code works fine on my old machine, so try this more thorough re-write:

; Script that attempts shutdown if computer is still on after 8pm
;#NoTrayIcon
#include <GUIConstants.au3>
Global $msg
Global $hGUIPrompt
Global $buttonCancel
Global $timestamp
Global $modeSwitch = 0
Global $BeginEffectivePeriod = 20; after 8 PM
Global $EndEffectivePeriod = 3; before 3 AM
While 1
    $hour = Number(@HOUR)
;If $hour > $BeginEffectivePeriod Or $hour < $EndEffectivePeriod Then
; if its later than 8pm and/or earlier than 3 am then ...
        Build_Controls()
        While 1
            $msg = GUIGetMsg()
            Sleep(60)
            If $msg = $buttonCancel Then
                Delete_Controls()
                ExitLoop 1
            EndIf
            If $modeSwitch And TimerDiff($timestamp) > 90000 Then Shutdown(1)
        WEnd
;EndIf
    Sleep(30000); checking the time TWICE A minute
WEnd

Func Build_Controls()
    $hGUIPrompt = GUICreate("Shutdown", 300, 150, 10, 10, $DS_MODALFRAME)
    GUICtrlCreateLabel("LANDesk is shutting down your PC! Press 'Cancel' to cancel the shutdown.", 20, 10, 260, 40)
    $buttonCancel = GUICtrlCreateButton("Cancel", 100, 100, 100, 20)
    $timestamp = TimerInit()
    $modeSwitch = 1
;Run(@ComSpec & " /c shutdown -s -t 90", "", @SW_HIDE)
    Sleep(300)
    GUISetState()
EndFunc;==>Build_Controls

Func Delete_Controls()
     $modeSwitch = 0
;Run(@ComSpec & " /c shutdown -a", "", @SW_HIDE)
    Sleep(300)
    GUIDelete($hGUIPrompt)
    Exit
EndFunc;==>DeleteControls

One other option for you is to use the code I last posted, but swap out the Run command line with this one:

Run(@ComSpec & " /c shutdown -s -t 90", @WindowsDir & "\system32", @SW_HIDE)

The second parameter assumes that the shutdown.exe file is in that folder on your system. Do a search of your Windows directory for it, looking in subdirectories and hidden folders (in the case where you just can't live without that lovely thing).

And shaunex - Welcome to the world-famous AutoIt Forum - Established Since 1900

Edited by Squirrely1

Das Häschen benutzt Radar

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  

×