本文共 7112 字,大约阅读时间需要 23 分钟。
作为管理员,偶尔会被一些不按常理出牌的用户弄得很无奈,按说,大家下班后,是不是您得把计算机关闭了再走?时下,大家都在喊低碳环保节能,这就是践行的一个实际行动啊。再说,计算机不关闭,明显地这也是一个消防安全隐患!
某天深夜,某位领导回家路过公司的一排排办公室,那微略的灯光在黑暗中是如此的光亮。第二天,被领导谈话......在此省去一千字.
随后公司首先发文进行了行政告示,但免不了总有粗心的MM下班为了赶公司班车还是忘记关闭计算机!看来只有靠偶来看好最后一关了!
实现思路:
比如每天晚上23:00 准时.用循环的思路遍历公司所有的IP,ping它,若在线,就 shutdown -m \\ip -f -s -t 0, 最后不要忘记记下日志,留做第二天领导查的上交“作业”或者再主动去告诉一下那个粗心的家伙!
- For $i=0 To 255
- For $j=0 To 255
-
- If $i=40 And $j=147 Then ExitLoop ;呵呵,记得把自己排除一下
-
- ToolTip("正在核查:"&"172.16."&$i&"."&$j, @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)
- If Ping("172.16."&$i&"."&$j,250) then
- $sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0"
- Runwait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
- _log("在线:172.16."&$i&"."&$j)
- EndIf
- Sleep(50)
- Next
- Next
如上所示,注意事项,记得将自己临时排除一下,等脚本全部执行完了,最后自己来关机:
- shutdown(5)
日志部分的构造:
- $CountLines =_FileCountLines($filelog)
- If @error Then Exit
-
- For $i=1 To $CountLines
- $tmpIP=StringRegExpReplace(FileReadLine ($filelog,$i), '.*在线:([^\z]+).*', '$1')
- If StringRegExp ($tmpIP, "((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)") Then
- ToolTip("正在核查"&$tmpIP&"的执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)
- If Not Ping($tmpIP) Then
- _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 已关闭!", 1)
- Else
- _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 未关闭!", 1)
- EndIf
- EndIf
- Next
- Exit(MsgBox(262144+64,"完成-8秒后自动退出","可查看日志文件:"&$filelog&"查看执行情况",8))
-
当然,您若有更好的更直观的构造欢迎多多指点。
关于如何在每天晚上23:00来执行这个脚本。想必大家已经想到了:系统里的“任务计划”,挂在上边即可。当然挂在域控上也成,挂在自己的机器上也行,但要求一定要使用域管理员来登录来执行!似乎费话了!
最后开源附上我根据我公司实际情况来的,请路过的朋友多多指教:
- #NoTrayIcon
- #Region ;**** 参数创建于 ACNWrapper_GUI ****
- #AutoIt3Wrapper_icon=c:\WINDOWS\system32\SHELL32.dll
- #AutoIt3Wrapper_outfile=C:\Documents and Settings\zhanghong1\桌面\全体关机.exe
- #AutoIt3Wrapper_Compression=4
- #AutoIt3Wrapper_Res_Comment=管理员专用
- #AutoIt3Wrapper_Res_Description=zhanghong1@ceri.com.cn
- #AutoIt3Wrapper_Res_Fileversion=2011.3.29.1
- #AutoIt3Wrapper_Res_LegalCopyright=zhanghong1@ceri.com.cn
- #EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
- #Include <File.au3>
-
-
- HotKeySet("{ESC}", "Terminate")
-
- Global $filelog="\\172.16.128.50\Backup\shutdown_log\"&@YEAR&"-"&@MON&"-"&@MDAY&"_log.txt"
- If Not FileExists($filelog) then
- _FileCreate($filelog)
- If @error Then Exit(MsgBox(262144+16,"严重错误:","无法写入:"&$filelog))
- EndIf
-
- If @UserName<>"zhanghong1" and @UserName<>"zhouyu" Then Exit(MsgBox(262144 +16,"出错","拒绝非管理员运行"))
- If @HOUR<23 Then
- If MsgBox(262144+17,"本机时钟:"HOUR,"强烈建议在晚11点后再考虑检查全体关机!要不后果严重,对吧?",55)=1 Then exit
- EndIf
-
- For $i=0 To 255
- For $j=0 To 255
- If $i=128 Or $i=136 Then ExitLoop
- If $i=40 And $j=147 Then ExitLoop
- If $i=40 And $j=241 Then ExitLoop
- If $i=41 And $j=10 Then ExitLoop
- If $i=9 And $j=219 Then ExitLoop
-
- ToolTip("正在核查:"&"172.16."&$i&"."&$j, @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)
- If Ping("172.16."&$i&"."&$j,250) then
- $sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0"
- Runwait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
- _log("在线:172.16."&$i&"."&$j)
- EndIf
- Sleep(50)
- Next
- Next
-
- ToolTip("正在核查执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)
- Sleep(8000)
- $CountLines =_FileCountLines($filelog)
- If @error Then Exit
-
- For $i=1 To $CountLines
- $tmpIP=StringRegExpReplace(FileReadLine ($filelog,$i), '.*在线:([^\z]+).*', '$1')
- If StringRegExp ($tmpIP, "((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)") Then
- ToolTip("正在核查"&$tmpIP&"的执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)
- If Not Ping($tmpIP) Then
- _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 已关闭!", 1)
- Else
- _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 非计算机装备?", 1)
- EndIf
- EndIf
- Next
- Exit(MsgBox(262144+64,"完成-8秒后自动退出","可查看日志文件:"&$filelog&"查看执行情况",8))
-
-
- func taskbarh()
- $pos = WinGetPos("[class:Shell_TrayWnd]")
- if isarray($pos) Then
- return $pos[3]
- EndIf
- EndFunc
-
- Func Terminate()
- Exit(MsgBox(262144+16,"您按了 ESC 键?","点击 确定 强制退出!"))
- EndFunc
-
-
- func _log($varjiamiwr)
- _FileWriteLog($filelog,$varjiamiwr)
- EndFunc
---
我的调试平台:
2011.6.3更新更完美的:
- #include <File.au3>
- #include <inet.au3>
-
-
- HotKeySet("{ESC}", "Terminate")
-
- Global $filelog = "\\172.16.128.50\Backup\shutdown_log\" & @YEAR & "-" & @MON & "-" & @MDAY & "_log.txt", $i, $j
- Global $Remoteuser = "domain\administrator" ;远程关机所用管理员用户
- Global $Remotepass = "BJp@ssw0rd" ;远程关机所用管理员密码
-
-
- If Not FileExists($filelog) Then
- _FileCreate($filelog)
- If @error Then Exit (MsgBox(262144 + 16, "严重错误:", "无法写入:" & $filelog))
- EndIf
-
- If @UserName<>"zhanghong1" Or @UserName<>"administrator" Then Exit(MsgBox(262144 +16,"出错","拒绝非授权管理员运行"))
-
- If @HOUR < 17 And @HOUR > 8 Then
- ;防止在工作时间误运行本程序,当然若坚持运行也是可以的。。。。
- If MsgBox(262144 + 17, "本机时钟:" & @HOUR, "强烈建议在非工作时间里再考虑检查遍历全体关机!要不后果严重,对吧?", 55) = 1 Then Exit
- EndIf
-
- $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Initialize a COM error handler
- $objlocator = ObjCreate("wbemscripting.swbemlocator")
- If Not IsObj($objlocator) Then
- MsgBox(262144 + 16, "8秒后自动退出", "无法创建对象: wbemscripting.swbemlocator", 8)
- _log("系统严重错误,无法创建对象: wbemscripting.swbemlocator")
- Exit
- EndIf
-
- For $i = 0 To 255
- For $j = 0 To 255
- If $i = 128 Or $i = 136 Then ExitLoop ;该段为7*24的服务器区域,故跳过。
- ToolTip("正在核查:" & "172.16." & $i & "." & $j, @DesktopWidth / 2, (@DesktopHeight - taskbarh() - 30), "稍候...", 1, 2)
- If Ping("172.16." & $i & "." & $j, 250) Then
- ;$sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0"
- ;Run(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
- $objWMIService = $objlocator.connectserver("172.16." & $i & "." & $j, "root/cimv2", $Remoteuser, $Remotepass)
- If Not @error Then
- $colItems = $objWMIService.ExecQuery("SELECT * FROM win32_operatingsystem where primary= true")
- For $objItem In $colItems
- If ($i = 40 And $j = 241) Or ($i = 41 And $j = 10) Or ($i = 9 And $j = 219) Or ($i = 40 And $j = 147) Then
- $objItem.win32shutdown(6);重启
- If Not @error Then
- TCPStartup()
- $aResult = _TCPIpToName("172.16." & $i & "." & $j)
- If @error Then
- _log("->在线IP:172.16." & $i & "." & $j & " 已成功远程重启设备!")
- Else
- _log("->在线IP:172.16." & $i & "." & $j & " 机器名:" & $aResult & " 已成功远程重启设备!")
- EndIf
- TCPShutdown()
- EndIf
- Else
- $objItem.win32shutdown(5) ;关机
- If Not @error Then
- TCPStartup()
- $aResult = _TCPIpToName("172.16." & $i & "." & $j)
- If @error Then
- _log("→在线IP:172.16." & $i & "." & $j & " 成功远程关掉!")
- Else
- _log("→在线IP:172.16." & $i & "." & $j & " 机器名:" & $aResult & " 成功远程关掉!")
- EndIf
- TCPShutdown()
- EndIf
- EndIf
- Next
- EndIf
- EndIf
- Sleep(50)
- Next
- Next
-
- Exit (MsgBox(262144 + 64, "完成-8秒后自动退出", "可查看日志文件:" & $filelog & "查看执行情况", 5))
-
-
- Func taskbarh()
- $pos = WinGetPos("[class:Shell_TrayWnd]")
- If IsArray($pos) Then
- Return $pos[3]
- EndIf
- EndFunc ;==>taskbarh
-
- Func Terminate()
- Exit (MsgBox(262144 + 16, "您按了 ESC 键?", "点击 确定 强制退出!", 30))
- EndFunc ;==>Terminate
-
-
- Func _log($varjiamiwr)
- _FileWriteLog($filelog, $varjiamiwr)
- EndFunc ;==>_log
-
-
- ; This is my custom defined error handler
- Func MyErrFunc()
- $LOALERROR = $oMyError.description
- If StringInStr($LOALERROR, "RPC") Or StringInStr($LOALERROR, "拒绝") Then
- _log("172.16." & $i & "." & $j & "可能为非计算机装备(打印服务器网关等?),故无法执行远程操作!错误代码:" & Hex($oMyError.number, 8) & " " & $LOALERROR)
- Else
- _log("172.16." & $i & "." & $j & " 因:" & $LOALERROR & " 错误代码:" & Hex($oMyError.number, 8) & " 无法执行远程操作!")
- EndIf
- EndFunc ;==>MyErrFunc
转载地址:http://duqsa.baihongyu.com/