检测计划关闭 [英] Detecting a scheduled shutdown

查看:28
本文介绍了检测计划关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 cmd 脚本将执行一组补丁,如果需要重新启动以避免补丁问题,它被设计为中止.如果计划重新启动(例如,通过关闭"命令),我还想将脚本扩展为中止,以避免在可能的情况下在补丁中重新启动.不幸的是,除了尝试安排另一次关机之外,我还没有设法找到一种方法来检测这种情况,这会导致:

I have a cmd script that will execute a set of patches, and it's designed to abort if a reboot is required to avoid patching issues. I'd also like to extend the script to abort if a reboot is scheduled (E.g. via the "shutdown" command) in order to avoid reboots mid patch if possible. Unfortunately I haven't managed to find a way to detect this apart from attempting to schedule another shutdown, which results in:

已安排系统关闭.(1190)

A system shutdown has already been scheduled.(1190)

虽然理论上我可以使用它,但我认为每次需要检查计划的重启时都吓唬登录用户并不是一个好习惯.我真正需要的是一种查询由关闭"命令修改的状态的方法.

While I could theoretically use this I don't think it would be good practice to scare logged in users every time I needed to check for a scheduled reboot. What I really need is a way to QUERY the state modified by the "shutdown" command.

这可能吗?对于任何不涉及让我在系统上永久运行应用程序以捕获关闭事件(我认为在实际触发关闭之前甚至不会发送)的解决方案,我都会感到满意

Is this possible? I'll be happy with really any solution that doesn't involve me having an application running permanently on the system to catch the shutdown events (which I don't think even get sent around until the shutdown is actually triggered)

推荐答案

我也遇到了同样的问题,找遍了,没找到有用的.

I have the same problem, I searched all over, but did not find anything useful.

最终,我开始胡思乱想我能想到的不同事情.对于我们的问题,我可以想到两种解决方法.

Eventually I just started messing around with different things I could think of. I can think of two workarounds for our problem.

事件查看器可以告诉您已安排关机,但不会告诉您安排时间.

The event viewer can tell you that a shutdown has been scheduled, but not WHEN it was scheduled.

一个想法是在事件查看器中查询最近的系统关闭、最近的计划关闭和最近取消的计划关闭.如果预定的关闭比最近的取消或关闭更近,则有一个正在进行中.事件 ID 是:1074 用于开始计划关闭,1075 用于取消计划关闭,在 Windows 日志/系统下12 系统启动13 用于系统关闭(均在 Windows 日志/系统下)

One idea is to query the event viewer for the most recent system shutdown, the most recent scheduled shutdown, and the most recent cancellation of a scheduled shutdown. If the scheduled shutdown is more recent than either the most recent cancellation or shutdown then there is one in progress. The Event IDs are: 1074 for a started scheduled shutdown, 1075 for a canceled scheduled shutdown, under Windows Logs/System 12 for system start up 13 for system shutdown (all under Windows Logs/System)

我懒得在批处理文件中执行此操作,但我知道这是可能的.

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

所以这就是我最终做的:与其安排关机,不如尝试中止它.如果没有进行关机,它会抛出一个错误(无法中止系统关机,因为没有关机正在进行.(1116)).我认为这比安排关机吓坏用户要好得多.

So this is what I ended up doing: Rather than schedule a shutdown, just try to abort it. If no shutdown is in progress it will throw an error (Unable to abort the system shutdown because no shutdown was in progress.(1116)). Much better I think than freaking out a user by scheduling a shutdown.

这是我为一个简单的自动关机脚本编写的代码副本,它在取消和开始计划关机之间切换.

Here is a copy of the code I wrote for a simple auto shutdown script, it toggles between cancelling 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 revisiting this answer as I have more info to add.

上述解决方案将使用/t 参数通过 shutdown.exe 命令检测是否已安排关机.

The Above solution will detect if a shutdown has been scheduled via the shutdown.exe command using the /t argument.

如果您需要确定 Windows 是否安排了关机(就像它在某些 Windows 更新后自动执行的那样),那么可以设置一个注册表项供您查询.下面是在 PowerShell 中,但您可以编写一个批处理文件来执行相同的操作.

If you need to determine if Windows has schedule a shutdown (as it does automatically after some Windows Updates) then there is a registry entry that is set which you can query. The below is in PowerShell, but you can write a batch file to do the same.

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

注意:如果使用shutdown.exe计划重启,则不会返回true,只有在Windows提示您的计算机需要重启时才会返回true.

Note: this will not return true if a reboot in scheduled using shutdown.exe, it will only return true if Windows says your computer need to be rebooted.

这篇关于检测计划关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆