Last active
November 9, 2023 01:26
-
-
Save chenshaoju/ebbf7542ffaa8afc776a90f50959cf35 to your computer and use it in GitHub Desktop.
Windows停电自动关机批处理脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
REM 注意:此脚本需要在执行的计算机具备UPS为前提的条件下进行,且目标IP设备不能具有UPS。 | |
REM 此批处理可以检测目标IP是否因为停电而失效,一旦失效则在180秒后关机。 | |
REM 需将此批处理写入计划任务才能使用,建议每隔60秒执行一次。 | |
REM 若不知道如何配置计划任务,请参阅 https://twitter.com/chenshaoju/status/973109556640333825 | |
REM 若参照60秒执行一次的话,在120至180秒内若恢复供电(能Ping通),则会取消关机。 | |
REM 若路由器或需要检测的目标IP不是 192.168.1.1 ,请手动修改下方的 192.168.1.1 IP地址。 | |
REM 关机等待时间不建议设置太短,避免手动开机但目标IP未启动时又立刻自动关机。 | |
REM 若只需要监控一个设备(默认),请勿注释下一行。 | |
ping /n 3 /l 1 192.168.1.1|findstr "TTL=" && ( goto PowerOn ) || goto PowerOff | |
REM 若需要监控两个(或多个,请自行参考格式添加)设备(可选),请取消注释下一行(删除开头的 "REM " ),并注释或删除上一行,此行是“和”(AND)的逻辑情况,只有两个IP都失效的情况下才会执行关机。 | |
REM ping /n 3 /l 1 192.168.1.1|findstr "TTL=" && ( goto PowerOn ) || ping /n 3 /l 1 192.168.1.2|findstr "TTL=" && ( goto PowerOn ) || goto PowerOff | |
:PowerOn | |
shutdown /a | |
exit 0 | |
:PowerOff | |
shutdown /s /t 180 | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感觉用小括号断行也行