Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I use the Group Policy Editor which is part of Windows 7 (also of Windows XP) to run a so-called shutdown script, which will automatically be executed each time the system is shutdown or rebooted. My problem is: I need to know in my script if the user has selected to shutdown the system, or if he has selected reboot instead. Both actions will make Windows run the shutdown script, but how can I determine during that script execution which action was actually performed?

Is there any way to know, during shutdown, if the system currently performs a shutdown or a reboot?

like image 868
Erik Avatar asked May 21 '12 09:05

Erik


People also ask

How do you determine if there is a pending reboot?

Testing for a a Pending Reboot (The Easy Way) Simply open up your PowerShell console and type Install-Script Test-PendingReboot . Install-Script will download my PowerShell script from the PowerShell Gallery to C:\Program Files\WindowsPowerShell\Scripts.

What is shutdown command in CMD?

shutdown /s command will shut down the system you are working on. shutdown / i from the command prompt will open the Remote Shutdown Dialog window, this will provide options available via an easy to use interface including networked systems.

Is Windows pending a reboot?

The message "A pending reboot has been detected" indicates that there are updates pending on the machine and a reboot is required prior to any additional updates being performed on the workstation.


1 Answers

On pre-vista systems you can query the Registry:

The Shutdown Setting DWORD found under HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer stores the setting selected most recently from the list on the Shut Down Windows dialog box for the current user.

On more recent systems, you can query the System Eventlog in your shutdown script, like this:

$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"}
    }
like image 162
jon Z Avatar answered Nov 15 '22 04:11

jon Z