把脚本放到 c:\windows\system32\,执行 shut -i 安装生成任务计划,实现超1小时关机,开机后检查如果未达到1小时再次关机,为了减少内容,忽略了闰年问题
@echo off
setlocal enabledelayedexpansion
if /i "%1"=="-i" (goto :INSTALL)
cd /d c:\windows\system32
::计算当前时间值
call :Date2days %date%
for /f "tokens=1,2 delims=:" %%a in ("%time%") do (set /a now=%%a*60+%%b)
set /a now=(%days%+%day%-1)*1440+%now%
::计算启动时间
for /f "tokens=2,3 delims=, " %%a in ('systeminfo^|findstr /i /c:"系统启动时间"') do (
set startdate=%%a
set starttime=%%b
)
call :Date2days %startdate%
for /f "tokens=1,2 delims=:" %%a in ("%starttime%") do (set /a startminutes=%%a*60+%%b)
set /a closetime=(%days%+%day%-1)*1440+%startminutes%+60
::对比开机时间和当前时间
if /i %now% gtr %closetime% (
echo 开机已经超过1小时,立即关机
echo %now% >shutdown.txt
shutdown /s /t 0 /f
goto :eof
)
if not exist shutdown.txt (
echo %now% >shutdown.txt
goto :eof
)
for /f "tokens=*" %%a in (shutdown.txt) do (set /a last=%%a+60)
if /i %now% leq %last% (
echo 此次开机距上次关机未超过1小时,立即关机
echo %now% >shutdown.txt
shutdown /s /t 0 /f
)
goto :eof
:Date2days
for /f "tokens=2,3 delims=/- " %%a in ("%1") do (
set month=%%a
set day=%%b
)
set 1=0
set 2=31
set 3=28
set 4=31
set 5=30
set 6=31
set 7=30
set 8=31
set 9=31
set 10=30
set 11=31
set 12=30
set days=0
for /l %%a in (1,1,%month%) do (set days=!%%a!+!days!)
goto :eof
:INSTALL
schtasks /create /tn shutdown /tr c:\windows\system32\shut.bat /ru system /f /sc minute /mo 5
[ Last edited by tongzj on 2016-2-21 at 17:48 ]