Run a Script at Shutdown (OnShutdown): Unterschied zwischen den Versionen
Admin (Diskussion | Beiträge) K (Admin verschob die Seite OnShutdown nach Run a Script before Shutdown (OnShutdown), ohne dabei eine Weiterleitung anzulegen) |
Admin (Diskussion | Beiträge) |
||
(9 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
+ | Unfortunately Windows does not bring a simple option to run a script/command at shutdown. There are some ways to do this, but all of this ways are no reliable and does not always works if you use fastboot. | ||
+ | Unreliable methods of running script/command at shutdown: | ||
+ | *'''Task Scheduler''' event trigger on event id 1074/6006/6008 (<Select Path="System">*[System[Provider[@Name='User32'] and EventID=1074]]</Select>) depends on EventLog and TaskScheduler and doesn't fire reliably (or at all on Server Core 1909) | ||
+ | *'''Group Policy Shutdown''' can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and isn't supported on Server Core 1909 | ||
+ | *'''Register-WmiEvent -Class Win32_ComputerShutdownEvent''' can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and fails on Server Core 1909 | ||
+ | == OnShutdown == | ||
+ | After burning some hours and testing all the methods above, I found a interesting GitHub Projekt '''[https://github.com/gfody/OnShutdown OnShutdown]'''. | ||
+ | === About OnShutdown === | ||
+ | ''This is a reliable way to run code at shutdown on Windows. ''<br> | ||
+ | ''A service that accepts '''SERVICE_CONTROL_PRESHUTDOWN''' is allowed to delay the system shutdown up to 125 seconds (provided it asks for it).'' <br> | ||
+ | ''The 125 second limit can be extended by editing HKLM:\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout. See Service Control Handler Function for more details.'' | ||
+ | === How to use -> Register a Windows Service "OnShutdown" === | ||
+ | '''Example: run a PowerShell command:''' | ||
+ | <source lang="powershell"> | ||
+ | New-Service OnShutDown -bin 'C:\Temp\OnShutdown.exe 30000 "powershell -noninteractive -noprofile -command <YOUR_COMMAND>"' -StartupType Automatic | ||
+ | Start-Service OnShutDown | ||
+ | </source> | ||
+ | '''Example: run a PowerShell script:''' | ||
+ | <source lang="powershell"> | ||
+ | New-Service OnShutDown -bin 'C:\Temp\OnShutdown.exe 30000 "powershell -noninteractive -noprofile -file C:\Temp\<YOUR_SCRIPT>.ps1"' -StartupType Automatic | ||
+ | Start-Service OnShutDown | ||
+ | </source> | ||
+ | ''More information's: https://github.com/gfody/OnShutdown'' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | [[Kategorie:Windows]] | ||
[[Kategorie:Tools]] | [[Kategorie:Tools]] |
Aktuelle Version vom 30. September 2021, 07:51 Uhr
Unfortunately Windows does not bring a simple option to run a script/command at shutdown. There are some ways to do this, but all of this ways are no reliable and does not always works if you use fastboot.
Unreliable methods of running script/command at shutdown:
- Task Scheduler event trigger on event id 1074/6006/6008 (<Select Path="System">*[System[Provider[@Name='User32'] and EventID=1074]]</Select>) depends on EventLog and TaskScheduler and doesn't fire reliably (or at all on Server Core 1909)
- Group Policy Shutdown can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and isn't supported on Server Core 1909
- Register-WmiEvent -Class Win32_ComputerShutdownEvent can be terminated early, doesn't fire when fastboot is enabled (default in Win10+) and fails on Server Core 1909
OnShutdown
After burning some hours and testing all the methods above, I found a interesting GitHub Projekt OnShutdown.
About OnShutdown
This is a reliable way to run code at shutdown on Windows.
A service that accepts SERVICE_CONTROL_PRESHUTDOWN is allowed to delay the system shutdown up to 125 seconds (provided it asks for it).
The 125 second limit can be extended by editing HKLM:\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout. See Service Control Handler Function for more details.
How to use -> Register a Windows Service "OnShutdown"
Example: run a PowerShell command:
New-Service OnShutDown -bin 'C:\Temp\OnShutdown.exe 30000 "powershell -noninteractive -noprofile -command <YOUR_COMMAND>"' -StartupType Automatic Start-Service OnShutDown
Example: run a PowerShell script:
New-Service OnShutDown -bin 'C:\Temp\OnShutdown.exe 30000 "powershell -noninteractive -noprofile -file C:\Temp\<YOUR_SCRIPT>.ps1"' -StartupType Automatic Start-Service OnShutDown
More information's: https://github.com/gfody/OnShutdown