In a Windows 7 script, how can I determine if the current system shutdown is actually a reboot?

I use the Group Policy Editor, which is included with Windows 7 (also Windows XP), to run the so-called shutdown script, which will be automatically executed every time the system is shut down or rebooted. My problem: I need to know in my script if the user chose to shut down the system or if he chose to reboot instead. Both actions will force Windows to run the shutdown script, but how can I determine at run time what script was actually performed?

Is there a way to find out during shutdown if the system is currently shutting down or rebooting?

+5
source share
2 answers

On pre-vista strong> systems, you can request the Registry :

The shutdown setting DWORD found in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorersaves the last selected one from the list in the "Shut Down Windows" dialog box for the current user.

On later systems, you can query the System event log in your disconnect script, for example:

$systemstateentry = get-eventlog -LogName system -Source User32 | ?{$_.eventid -eq 1074} | select -first 1

switch -regex ($systemstateentry.message) 
    { 
        ".*restart.*" {"restart"} 
        ".*power off.*" {"power off"} 
        default {"unknown"}
    }
+8
source

bash wevtutil.exe, , . script . Windows restart , . .

query='*[System[(EventID=1074) and TimeCreated[timediff(@SystemTime) <= 60000]]]'
current_shutdown=$(wevtutil qe system -c:1 -rd:true -f:xml -q:"$query")
rebooting=$(grep -iE "<data[^<>]*>restart</data>" <<<"$current_shutdown")

if [[ -n "$rebooting" ]]; then echo 'System is rebooting'
elif [[ -n "$current_shutdown" ]]; then echo 'System is shutting down'
else echo 'System is neither rebooting nor shutting down'; fi
+1

All Articles