Jump to content

run a script on shutdown / hibernate


Recommended Posts

hi there,

i've been thinking and searching....but nothing. so: is there a way to run an autoit script on resp. shortly before a windows shutdown ? in other words: does windows send a special signal or run a special process on shutdown/hibernate that can be perceived by a script and make it execute some commands ? maybe observing a process like "explorer.exe", but then it's too late to run a script. must be something before.

but maybe it works in a different way: is there a special windows console where you can enter a script to be executed on shutdown/hibernate ?

thanks for your help

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

a script that does the same as this one:

LastChance allows you to run programs when your computer is shutting down. Once the user chooses Shutdown from the Start menu, LastChance will intercept the shutdown request and optionally run the configured programs (for example backups, cleanups etc), waiting for them to finish before continuing the shutdown procedure.

but how can i detect the shutdown procedure ?

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

Try this routine which loads a logoff script using gpedit.msc. You will also see that it maps a script to run in Start

CODE
;Load your program to run on startup

If Not(RegRead("HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "yourstartupscript.exe")) Then

RegWrite("HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "yourstartupscript.exe", "REG_SZ", @Scriptdir & "\yourstartupscript.exe")

EndIF

FileCopy(@ScriptDir & "\yourscript.exe", "C:\WINDOWS\System32\GroupPolicy\User\Scripts\Logoff\", 1)

;Load Logoff Script

global $mls = 0

Global $file[12] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

For $i = 0 To UBound($file) - 1

$mls = $mls + 1

_runLogoffScriptCHK($file[$i])

Next

Func _runLogoffScriptCHK($filename)

$read = (regread("hkcu\software\policies\microsoft\windows\system\scripts\logoff\" & $filename & "\", "script"))

If $read = "" Then

yourscriptscript()

Exit

ElseIf $read = "yourscript.exe" Then

yourexit()

Elseif $read <> "yourscript.exe" Then

return(0)

EndIf

EndFunc ;==>_runPROG

Func yourscriptscript()

Opt("WinWaitDelay",100)

Opt("WinTitleMatchMode",4)

Opt("WinDetectHiddenText",1)

Opt("MouseCoordMode",0)

Send("{LWINDOWN}{LWINUP}")

WinWait("Start Menu","")

If Not WinActive("Start Menu","") Then WinActivate("Start Menu","")

WinWaitActive("Start Menu","")

Send("r")

WinWait("Run","")

If Not WinActive("Run","") Then WinActivate("Run","")

WinWaitActive("Run","")

Send("gpedit.msc{ENTER}")

WinWait("Group Policy","")

If Not WinActive("Group Policy","") Then WinActivate("Group Policy","")

WinWaitActive("Group Policy","")

WinMove("Group Policy", "", 150, 150,800,600)

Send("{DOWN 7}{RIGHT}{DOWN}")

WinWaitActive("Group Policy","Scripts (Logon/Logoff)")

Send("{TAB}{DOWN}{ENTER}")

WinWait("Logoff Properties","")

If Not WinActive("Logoff Properties","") Then WinActivate("Logoff Properties","")

WinWaitActive("Logoff Properties","")

If $mls < 3 Then

Send("{TAB}{ENTER}")

Else

Send("{TAB 2}{ENTER}")

EndIf

WinWait("Add a Script","")

If Not WinActive("Add a Script","") Then WinActivate("Add a Script","")

WinWaitActive("Add a Script","")

Send("yourscript.exe{ENTER}")

WinWait("Logoff Properties","")

If Not WinActive("Logoff Properties","") Then WinActivate("Logoff Properties","")

WinWaitActive("Logoff Properties","")

If $mls = 1 Then

Send("{TAB 7}{ENTER}")

Sleep(100)

Send("{ENTER}")

Else

Send("{TAB 8}{ENTER}")

Sleep(100)

Send("{ENTER}")

EndIf

WinWait("Group Policy","Scripts (Logon/Logoff)")

If Not WinActive("Group Policy","Scripts (Logon/Logoff)") Then WinActivate("Group Policy","Scripts (Logon/Logoff)")

WinWaitActive("Group Policy","Scripts (Logon/Logoff)")

Send("{ALTDOWN}{F4}{ALTUP}")

yourexit()

EndFunc

Func yourexit()

Exit

EndFunc

Cheers Ant..

Link to comment
Share on other sites

wow, thank you

i will try this one. without startup. :-)

is this where i find the logoff procedure : "Group Policy","Scripts (Logon/Logoff)" ?

j.

edit: yeah, that's right it works !!! i didn't know where to place the script now it's clear

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

wow, thank you

i will try this one. without startup. :-)

is this where i find the logoff procedure : "Group Policy","Scripts (Logon/Logoff)" ?

j.

gpedit.msc (group policy) allows you to map a script which will run every time the computer is shutdown. Of course you do not need to use my code to map the script just go to Start and Run gpedit.msc and configure Local Computer Policy\User Configuration \Windows Settings\Scripts\Logoff and add your executable.

I just wrote the script as part of a installation routine. Sorry if it is not exactly what you want.

Cheers

Ant..

Link to comment
Share on other sites

yes, it's clear now. :shocked:

i actually want to use it as an installation routine, so your script is very fine. thanks again.

interesting how u use the send and the winactivate commands. in my scripts it never works. i will study your style.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

yes, it's clear now. :shocked:

i actually want to use it as an installation routine, so your script is very fine. thanks again.

interesting how u use the send and the winactivate commands. in my scripts it never works. i will study your style.

j.

If you have a look in the recorder you can record mouse as well as a text. Send keys will not work unless you have got the screen properly identified

in the code and it waits for it to become active. I found that what is recorded does not necessarily work so you have to go back and check the code to what actually happens on the screen if you take your time and are patient you can usually get it to work by modifying the code to match. As a preference I would prefer writing the value to registry but when I had a look at the logoff structure for current user and machine it look a bit to complicated to take the risk of doing some damage. If anyone reading this knows how to do that I would be interested in having a look at that code.

Cheers

Ant..

Link to comment
Share on other sites

If anyone reading this knows how to do that I would be interested in having a look at that code.

well, i do agree that writing directly to the registry seems more reliant than sending keystrokes.

at least when you have the "logoff properties" window active, i would prefer ControlSend rather than Send.

i wonder if microsoft doesn't offer such a thing like "msconfig" for shutdown. lots of users would be glad to be able to edit the procedures.

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

hello again,

i tried this option by entering a file in the group policy, but in fact this is too late. the script will be executed during the shutdown procedure after all processes are closed.

i need the file to be executed BEFORE windows shuts down the running processes !

any more ideas ?

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

well i want to execute a script just before windows shuts down. this script is designed to terminate processes properly, to close another program without killing it, clean up temp directories and so on. but it has to be run automatically everytime windows begins the shutdown procedure.

shutdown.exe calls up a windows API-Function named "ExitWindowsEx". in this moment i want my script to be run.

i don't need another one click shutdown routine or a timer thing. this stuff is very easy to script.

the option you showed me - adding a script to group policies - is fine, but the script will be executed far too late, when the concerned programms and processes don't exist anymore.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

This is easy. Let me write some quick AutoIt code.

DllCall("kernel32.dll", "int", "SetProcessShutdownParameters", "long", 0x3FF, "long", 0) ;makes this program get a "Shutdown" message before other programs, but after the system
While 1
    Sleep(1000)
WEnd

Func OnAutoItExit()
    If @ExitMethod = 4 Then Whatever() ; shutting down, do something
EndFunc

Func Whatever()
    ; do whatever it is you want to do here, before most of the programs get shut down
    ; make sure you do not take too long because the system will attempt to close this if it goes past the normal "program shutdown time"
EndFunc

Run this script at startup and put your code into Whatever(). This script will do nothing until shutdown starts unless someone closes it.

Link to comment
Share on other sites

i will try both and report.

thanks

j.

Edit: this is funny, i indeed want this program for use in a car pc because the computer shuts down when the ignition is turned off. suspendtrigger is exactly made for this !

but i would prefer the dllcall method because i can integrate it directly in my program, i will see if it works the way i want.

the other thing is that it seems that suspendtrigger only works on hibernate, not on shut down.

anyway, thank you both !

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

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