[VBS脚本]定时提示并关闭计算机

时间:2021-08-09 14:03:21

一、Natalia.bat

双击这个文件可以运行Natalia

控制台在打开后会变成最小化,标题为PowerAssistant,黑色背景淡绿色文字

运行脚本Natalia.vbs

 @ECHO OFF 

 REM 脚本最小化运行.
IF "%1" == "m" GOTO BEGIN
MSHTA VBScript:CreateObject("WSCript.Shell").Run("%~fs0 m",)(Window.Close)&&EXIT
:BEGIN REM 注释最后一定要跟一个解释器可识别半角字符,否则脚本会运行错误.
REM 设置命令行标题.
TITLE PowerAssistant REM 设置命令行颜色.
REM 前景色(0=黑,1=蓝,2=绿,3=浅绿,4=红,5=紫,6=黄,7=白,8=灰,9=淡蓝).
REM 背景色(A=淡绿,B=淡浅绿,C=淡红,D=淡紫,E=淡黄,F=亮白).
COLOR 0A REM 调用倒计时脚本.
CSCRIPT Natalia.vbs //NOLOGO PAUSE

二、Natalia.vbs

在最上面的“定义常量部分”,可以设置预计关机的时间

 Option Explicit

 '定义常量
Const ObjHour = '时 0-23
Const ObjMinute = '分 0-59
Const ObjSecond = '秒 0-59 '检查输入是否合法
Dim IsInputLegal
IsInputLegal = True
If ObjHour < Or ObjHour > Then
IsInputLegal = False
End If
If ObjMinute < Or ObjMinute > Then
IsInputLegal = False
End If
If ObjSecond < Or ObjSecond > Then
IsInputLegal = False
End If
If IsInputLegal = False Then
WScript.Echo "输入非法 时区间:[0-23] 分区间:[0,59] 秒区间:[0,59]"
WScript.Quit
End If '定义变量
Dim ObjTime '目标时间
Dim CurTime '当前时间
Dim CurHour '当前时
Dim CurMinute '当前分
Dim CurSecond '当前秒 ObjTime = "目标时间:" & ObjHour & "时 " & ObjMinute & "分 " & ObjSecond & "秒" Dim IsTimeOut
IsTimeOut = False '时间到则终止循环
Do Until IsTimeOut = True CurHour = Hour(Now)
CurMinute = Minute(Now)
CurSecond = Second(Now) CurTime = "当前时间:" & CurHour & "时 " & CurMinute & "分 " & CurSecond & "秒"
WScript.Echo ObjTime & " | " & CurTime If (CurHour = objHour And CurMinute = objMinute And CurSecond = objSecond) Or _
(Hour(Now) = objHour And Minute(Now) = objMinute And Second(Now) = objSecond+) Then
IsTimeOut = True
End If WScript.Sleep() Loop WScript.CreateObject("wscript.shell").Run "shutdown -s -t 300",
WScript.Echo vbCrlf
WScript.Echo "收拾收拾准备睡觉啦!5分钟后我会把你的电脑关掉哦~~~"
WScript.Echo "在控制台输入命令 shutdown -a 可以取消关机计划"
WScript.Echo vbCrlf

三、运行效果

[VBS脚本]定时提示并关闭计算机