VBScript(Microsoft Visual Basic Script Edition).,微软公司可视化BASIC脚本版). 正如其字面所透露的信息, VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言。开发人员通常简称VBS,俗称VBS脚本。
GoTO_Sleep_v2.vbs脚本,备注:能在XP、Windows7、Windows 2000 Professional操作系统下执行,服务器系统不运行。
'关机脚本
On Error Resume Next
Dim objShell,intReturn,mbFinished,moWindow
Set objShell = CreateObject("Wscript.Shell")
'Set moWindow = WScript.CreateObject("InternetExplorer.Application","IE_")
mbFinished = False Dim OSVersion
Dim OSCaption
Dim MachineType MachineType = TestMachineType() If MachineType = "Laptop" Then
'笔记本不执行该策略
WScript.Quit End If OSCaption=GetOSCaption()
If (InStr(OSCaption, "Windows XP") Or InStr(OSCaption, "Windows 2000 Professional") Or InStr(OSCaption,"Windows 7")) Then
Call Main() '操作系统版本为XP,才执行
Else
intReturn = objShell.Popup("服务器系统,不尝试关机,5秒后自动退出关机程序",5, "服务器系统不进入关机状态......")
WScript.Quit
'WScript.Echo "不是Windows XP系统!不修改电源管理方案"
End If '==================================主程序结束============================================= Sub Main()
Do
time1=time intReturn = objShell.Popup("系统即将在30分钟后进入关机状态,如需继续使用电脑,请点击【确定】",1805, "系统将在30分钟后进入关机状态......") time2=time
time3=DateDiff("s",time1,time2)
'if
If time3>1800 Then
Go_Sleep()
Exit do
Else
Set moWindow = WScript.CreateObject("InternetExplorer.Application","IE_")
Call Wait_Sleep()
End If Loop
End Sub '================主程序完成================ Sub Go_Sleep() Const Shutdown_Mod = 5 'Forced Shutdown (1 + 4)
Set objNet = WScript.CreateObject( "WScript.Network" )
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate," & _
"(Shutdown)}!\\" & objNet.ComputerName & "\root\cimv2") '获得对象
Set colOSes = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOS In colOSes '只可能有一个 objOS 在colOSes集合中
intReturn = objOS.Win32Shutdown(Shutdown_Mod)
If intReturn <> 0 Then
msgbox "您有文档未保存,关机操作失败"
End If
Next WScript.Quit End Sub Sub Wait_Sleep()
dim i
mbFinished = False
i=0
'Const wshYes = 6
'Const wshNo = 7
'Const wshYesNoDialog = 4
'Const wshQuestionMark = 32 'intReturn = objShell.Popup("10分钟后进入关机状态,您是否需要取消今天的关机任务?点击【Yes】将取消关机", _
'600, "取消关机", wshYesNoDialog + wshQuestionMark) moWindow.Navigate "about:blank"
With moWindow.Document.ParentWindow.Document
.Write "<body scroll=no style='background-color:#d4d0c8;font-size:9pt'>10分钟后进入关机状态,您是否需要取消今天的关机任务?<br>点击【coding中,今天不关机了】将取消关机。还剩余<font id='str'>600</font>秒进入关机状态<br><br><div align='center'><input type='submit' value='coding中,今天不关机了' Width='10px' id='btnOK'/> <input type='button' value='1小时后再决定' class='cancel' id='btnCancel'/></div></body>"
.Title ="取消关机"
End With
moWindow.Document.Close With moWindow
.Toolbar = False
.Statusbar = False
.Menubar = False
.Resizable = False
.Width =500
.Height=130
.left= 350
.top= 350
Set .document.all.btnOK.onclick = GetRef("evtOK")
Set .document.all.btnCancel.onclick = GetRef("evtCancel")
.Visible = true
End With Do
WScript.Sleep 1000
i=i+1
if i<600 then
moWindow.Document.All.str.innerHTML = 600-i
If mbFinished Then Exit Do
else
Call Go_Sleep()
end if
Loop 'If intReturn = wshYes Then
'Wscript.Echo "You clicked the Yes button."
' WScript.Quit
'ElseIf intReturn = wshNo Then
'Wscript.Echo "You clicked the No button."等待30分钟后继续提示
' WScript.Sleep(1800000)
'Else
'WScript.Echo "The popup timed out."
' Call Go_Sleep()
'End If End Sub Sub evtOK
mbFinished = True
moWindow.Quit
Wscript.Quit
End Sub Sub evtCancel
mbFinished = True
moWindow.Visible = false
moWindow.Quit
'WScript.Sleep(5000)
WScript.Sleep(3600000)
End Sub Sub IE_onQuit
mbFinished = True
End Sub Function GetOSCaption() '返回计算机操作系统信息XP/Win2000/Win2003/Server等
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
GetOSCaption=objOS.Caption '返回操作系统的版本信息
Next End function Function TestMachineType() '查询计算机类型:台式机,笔记本等.....
Dim DevType
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
For Each strChassisType in objChassis.ChassisTypes
Select Case strChassisType
Case 3
DevType="Desktop"
Case 4
DevType="Desktop"
Case 6
DevType="Desktop"
Case 7
DevType="Desktop"
Case 8
DevType="Laptop"
Case 9
DevType="Laptop"
Case 10
DevType="Laptop"
Case 11
DevType="Laptop"
Case 12
DevType="Laptop"
Case 13
DevType="Laptop"
Case 14
DevType="Laptop"
Case Else
DevType="Other"
End Select
Next
Next
TestMachineType =DevType End Function
部署实施操作:
1.将GoTO_Sleep_v2.vbs脚本存放好,比如放在:C:\Windows\System32下
2.添加一个Windows计划任务,想要的效果是:周一周五每晚20:00自动执行此关机脚本
-打开Windows》操作中心》管理工具》计划任务,添加一个Windows计划任务
-添加一个触发器,设置什么时间触发执行,设置后效果比如:周一周五每晚20:00触发
-添加一个操作,设置读取我们要执行的脚本,设置后效果比如:C:\Windows\System32\GoTO_Sleep_v2.vbs
参考下设置完成效果图: