Scheduled outage detection

I have a cmd script that will execute a set of patches, and it is designed to be canceled if a reboot is required to avoid patch problems. I would also like to extend the script to cancel if a reboot is planned (for example, using the "shutdown" command) to avoid reloading the middle patch if possible. Unfortunately, I could not find a way to detect this separately from trying to schedule another shutdown, which leads to:

System shutdown is already planned. (1190)

While I could theoretically use this, I don’t think it would be a good practice to scare registered users every time I need to check a planned reboot. I really need a way to QUERY the state changed by the shutdown command.

Is it possible? I will really be glad for any solution that does not assume that an application that is constantly running in the system will catch shutdown events (which, it seems to me, are not even sent until the completion of the work is completed)

+8
source share
4 answers

I have the same problem, I was looking for everything, but did not find anything useful.

In the end, I just started messing around with various things that I could come up with. I can come up with two workarounds for our problem.

The event viewer can tell you that the completion was planned, but not when it was planned.

One idea is to ask the event viewer for the most recent shutdown of the system, the most recent scheduled shutdown, and the most recent cancellation of the scheduled shutdown. If a scheduled shutdown is later than the most recent cancellation or shutdown, then it is executed. Event IDs: 1074 to run on schedule, 1075 to cancel on schedule, in Windows / System 12 logs to start system 13 to shut down the system (all in Windows / System logs)

I was too lazy to do this in a batch file, although I know that this is possible.

Here's what I ended up doing: instead of scheduling a shutdown, just try to interrupt it. If the shutdown is not performed, an error message is displayed (Cannot interrupt the shutdown of the system because shutdown was not performed. (1116)). I think it’s much better than worrying the user when planning a shutdown.

Here is a copy of the code that I wrote for a simple automatic shutdown script, it switches between canceling and starting a scheduled shutdown.

@echo off shutdown /a if errorlevel 1 call:shutdown goto end :shutdown set /p choice=Shutdown in how many minutes? set /a mod=60 set /a mod*=%choice% shutdown /s /t %mod% :end 

EDIT - I am revising this answer as I have more information to add.

The above solution will detect if a shutdown is planned using the shutdown.exe command using the / t argument.

If you need to determine if Windows has a shutdown schedule (as it happens automatically after some Windows updates), then there is a given registry entry that you can request. Below is PowerShell, but you can write a batch file to do the same.

 Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending' 

Note: this will not return true, if a reboot is planned using shutdown.exe, it will return true only if Windows indicates that your computer needs to be restarted.

+1
source

I assume that you can use the Task Scheduler API to list scheduled tasks and see if any of them cause shutdown.exe. But I'm not sure that it is reliable. There may be several ways to schedule a shutdown.

0
source

Another way to do this, if your script sets the shutdown, is to save the% date% and% time% variables in global environments using the setx command. You can then check these variables the next time the script is called.

0
source

Perhaps you can try to initiate a shutdown by calling ExitWindowsEx with a large grace period and check the return code ERROR_SHUTDOWN_IS_SCHEDULED and call AbortSystemShutdown immediately if ExitWindosEx returns to success (indicating that you have successfully scheduled shutdown)

-1
source

Source: https://habr.com/ru/post/889233/


All Articles