做爲管理員,偶爾會被一些不按常理出牌的用戶弄得很無奈,按說,你們下班後,是否是您得把計算機關閉了再走?時下,你們都在喊低碳環保節能,這就是踐行的一個實際行動啊。再說,計算機不關閉,明顯地這也是一個消防安全隱患!sql
某天深夜,某位領導回家路過公司的一排排辦公室,那微略的燈光在黑暗中是如此的光亮。次日,被領導談話......在此省去一千字.安全
隨後公司首先發文進行了行政告示,但免不了總有粗心的MM下班爲了趕公司班車仍是忘記關閉計算機!看來只有靠偶來看好最後一關了!服務器
實現思路:app
好比天天晚上23:00 準時.用循環的思路遍歷公司全部的IP,ping它,若在線,就 shutdown -m \\ip -f -s -t 0, 最後不要忘記記下日誌,留作次日領導查的上交「做業」或者再主動去告訴一下那個粗心的傢伙!dom
- 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
如上所示,注意事項,記得將本身臨時排除一下,等腳本所有執行完了,最後本身來關機:ide
- shutdown(5)
日誌部分的構造:oop
- $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))
固然,您如有更好的更直觀的構造歡迎多多指點。google
關於如何在天天晚上23:00來執行這個腳本。想必你們已經想到了:系統裏的「任務計劃」,掛在上邊便可。固然掛在域控上也成,掛在本身的機器上也行,但要求必定要使用域管理員來登陸來執行!彷佛費話了!spa
最後開源附上我根據我公司實際狀況來的,請路過的朋友多多指教:調試
- #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
--- 個人調試平臺:http://autoit-cn.googlecode.com/files/AUTOIT_3.3.6.1-2.exe
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