不与你商量的远程强制关机

        作为管理员,偶尔会被一些不按常理出牌的用户弄得很无奈,按说,大家下班后,是不是您得把计算机关闭了再走?时下,大家都在喊低碳环保节能,这就是践行的一个实际行动啊。再说,计算机不关闭,明显地这也是一个消防安全隐患!

        某天深夜,某位领导回家路过公司的一排排办公室,那微略的灯光在黑暗中是如此的光亮。第二天,被领导谈话......在此省去一千字.

        随后公司首先发文进行了行政告示,但免不了总有粗心的MM下班为了赶公司班车还是忘记关闭计算机!看来只有靠偶来看好最后一关了!

实现思路:

比如每天晚上23:00 准时.用循环的思路遍历公司所有的IP,ping它,若在线,就 shutdown -m \\ip -f -s -t 0, 最后不要忘记记下日志,留做第二天领导查的上交“作业”或者再主动去告诉一下那个粗心的家伙!

 
 
  1. For $i=0 To 255  
  2.  For $j=0 To 255  
  3.  
  4.    If $i=40  And $j=147 Then ExitLoop  ;呵呵,记得把自己排除一下  
  5.  
  6.    ToolTip("正在核查:"&"172.16."&$i&"."&$j, @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)  
  7.    If Ping("172.16."&$i&"."&$j,250) then 
  8.      $sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0" 
  9.      Runwait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)  
  10.    _log("在线:172.16."&$i&"."&$j)  
  11.    EndIf  
  12.           Sleep(50)  
  13.  Next   
  14. Next 

如上所示,注意事项,记得将自己临时排除一下,等脚本全部执行完了,最后自己来关机:

 
 
  1. shutdown(5)  


日志部分的构造:

 
 
  1. $CountLines =_FileCountLines($filelog)  
  2. If @error Then Exit  
  3.  
  4. For $i=1 To $CountLines  
  5. $tmpIP=StringRegExpReplace(FileReadLine ($filelog,$i), '.*在线:([^\z]+).*''$1')  
  6. 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 
  7.    ToolTip("正在核查"&$tmpIP&"的执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)  
  8.    If Not Ping($tmpIP) Then 
  9.     _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 已关闭!", 1)  
  10.    Else 
  11.    _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&"  未关闭!", 1)  
  12.    EndIf     
  13. EndIf  
  14. Next 
  15. Exit(MsgBox(262144+64,"完成-8秒后自动退出","可查看日志文件:"&$filelog&"查看执行情况",8))  
  16.  

当然,您若有更好的更直观的构造欢迎多多指点。

关于如何在每天晚上23:00来执行这个脚本。想必大家已经想到了:系统里的“任务计划”,挂在上边即可。当然挂在域控上也成,挂在自己的机器上也行,但要求一定要使用域管理员来登录来执行!似乎费话了!

最后开源附上我根据我公司实际情况来的,请路过的朋友多多指教:

 
 
  1. #NoTrayIcon  
  2. #Region ;**** 参数创建于 ACNWrapper_GUI ****  
  3. #AutoIt3Wrapper_icon=c:\WINDOWS\system32\SHELL32.dll  
  4. #AutoIt3Wrapper_outfile=C:\Documents and Settings\zhanghong1\桌面\全体关机.exe  
  5. #AutoIt3Wrapper_Compression=4  
  6. #AutoIt3Wrapper_Res_Comment=管理员专用  
  7. #AutoIt3Wrapper_Res_Description=zhanghong1@ceri.com.cn  
  8. #AutoIt3Wrapper_Res_Fileversion=2011.3.29.1  
  9. #AutoIt3Wrapper_Res_LegalCopyright=zhanghong1@ceri.com.cn  
  10. #EndRegion ;**** 参数创建于 ACNWrapper_GUI ****  
  11. #Include <File.au3>  
  12.  
  13.  
  14. HotKeySet("{ESC}""Terminate")  
  15.  
  16. Global $filelog="\\172.16.128.50\Backup\shutdown_log\"&@YEAR&"-"&@MON&"-"&@MDAY&"_log.txt"  
  17.     If Not FileExists($filelog) then 
  18.        _FileCreate($filelog)  
  19.        If @error Then Exit(MsgBox(262144+16,"严重错误:","无法写入:"&$filelog))  
  20.     EndIf  
  21.  
  22. If @UserName<>"zhanghong1" and  @UserName<>"zhouyu" Then Exit(MsgBox(262144 +16,"出错","拒绝非管理员运行"))  
  23. If @HOUR<23 Then   
  24.   If MsgBox(262144+17,"本机时钟:"&@HOUR,"强烈建议在晚11点后再考虑检查全体关机!要不后果严重,对吧?",55)=1 Then exit  
  25. EndIf  
  26.  
  27. For $i=0 To 255  
  28.     For $j=0 To 255  
  29.       If $i=128 Or $i=136 Then ExitLoop  
  30.       If $i=40  And $j=147 Then ExitLoop  
  31.       If $i=40  And $j=241 Then ExitLoop  
  32.       If $i=41  And $j=10 Then ExitLoop  
  33.       If $i=9  And $j=219 Then ExitLoop  
  34.         
  35.       ToolTip("正在核查:"&"172.16."&$i&"."&$j, @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)  
  36.       If Ping("172.16."&$i&"."&$j,250) then 
  37.         $sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0" 
  38.         Runwait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)  
  39.          _log("在线:172.16."&$i&"."&$j)  
  40.       EndIf  
  41.       Sleep(50)  
  42.     Next      
  43. Next 
  44.  
  45. ToolTip("正在核查执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)  
  46. Sleep(8000)  
  47. $CountLines =_FileCountLines($filelog)  
  48. If @error Then Exit  
  49.  
  50. For $i=1 To $CountLines  
  51. $tmpIP=StringRegExpReplace(FileReadLine ($filelog,$i), '.*在线:([^\z]+).*''$1')  
  52. 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 
  53.    ToolTip("正在核查"&$tmpIP&"的执行情况", @DesktopWidth/2, (@DesktopHeight - taskbarh()-30),"稍候...", 1, 2)  
  54.    If Not Ping($tmpIP) Then 
  55.        _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 已关闭!", 1)  
  56.    Else 
  57.       _FileWriteToLine($filelog, $i, FileReadLine ($filelog,$i)&" 非计算机装备?", 1)  
  58.    EndIf     
  59. EndIf  
  60. Next 
  61. Exit(MsgBox(262144+64,"完成-8秒后自动退出","可查看日志文件:"&$filelog&"查看执行情况",8))  
  62.  
  63.  
  64. func taskbarh()  
  65. $pos = WinGetPos("[class:Shell_TrayWnd]")  
  66. if isarray($pos) Then 
  67.     return $pos[3]  
  68. EndIf  
  69. EndFunc  
  70.  
  71. Func Terminate()  
  72.     Exit(MsgBox(262144+16,"您按了 ESC 键?","点击 确定 强制退出!"))  
  73. EndFunc  
  74.  
  75.  
  76. func _log($varjiamiwr)  
  77.     _FileWriteLog($filelog,$varjiamiwr)  
  78. EndFunc 


           --- 我的调试平台http://autoit-cn.googlecode.com/files/AUTOIT_3.3.6.1-2.exe 

2011.6.3更新更完美的

 
 
  1. #include <File.au3>  
  2. #include <inet.au3>  
  3.  
  4.  
  5. HotKeySet("{ESC}""Terminate")  
  6.  
  7. Global $filelog = "\\172.16.128.50\Backup\shutdown_log\" & @YEAR & "-" & @MON & "-" & @MDAY & "_log.txt", $i, $j  
  8. Global $Remoteuser = "domain\administrator" ;远程关机所用管理员用户  
  9. Global $Remotepass = "BJp@ssw0rd" ;远程关机所用管理员密码  
  10.  
  11.  
  12. If Not FileExists($filelog) Then 
  13.     _FileCreate($filelog)  
  14.     If @error Then Exit (MsgBox(262144 + 16, "严重错误:""无法写入:" & $filelog))  
  15. EndIf  
  16.  
  17. If @UserName<>"zhanghong1" Or @UserName<>"administrator" Then Exit(MsgBox(262144 +16,"出错","拒绝非授权管理员运行"))  
  18.  
  19. If @HOUR < 17 And @HOUR > 8 Then 
  20.     ;防止在工作时间误运行本程序,当然若坚持运行也是可以的。。。。  
  21.     If MsgBox(262144 + 17, "本机时钟:" & @HOUR"强烈建议在非工作时间里再考虑检查遍历全体关机!要不后果严重,对吧?", 55) = 1 Then Exit  
  22. EndIf  
  23.  
  24. $oMyError = ObjEvent("AutoIt.Error""MyErrFunc") ; Initialize a COM error handler  
  25. $objlocator = ObjCreate("wbemscripting.swbemlocator")  
  26. If Not IsObj($objlocator) Then 
  27.     MsgBox(262144 + 16, "8秒后自动退出""无法创建对象: wbemscripting.swbemlocator", 8)  
  28.     _log("系统严重错误,无法创建对象: wbemscripting.swbemlocator")  
  29.     Exit  
  30. EndIf  
  31.  
  32. For $i = 0 To 255  
  33.     For $j = 0 To 255  
  34.         If $i = 128 Or $i = 136 Then ExitLoop ;该段为7*24的服务器区域,故跳过。  
  35.         ToolTip("正在核查:" & "172.16." & $i & "." & $j, @DesktopWidth / 2, (@DesktopHeight - taskbarh() - 30), "稍候...", 1, 2)  
  36.         If Ping("172.16." & $i & "." & $j, 250) Then 
  37.             ;$sCommand="shutdown -m \\172.16."&$i&"."&$j& " -f -s -t 0" 
  38.             ;Run(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)  
  39.             $objWMIService = $objlocator.connectserver("172.16." & $i & "." & $j, "root/cimv2", $Remoteuser, $Remotepass)  
  40.             If Not @error Then 
  41.                 $colItems = $objWMIService.ExecQuery("SELECT * FROM  win32_operatingsystem where primary= true")  
  42.                 For $objItem In $colItems  
  43.                     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 
  44.                         $objItem.win32shutdown(6);重启  
  45.                         If Not @error Then 
  46.                             TCPStartup()  
  47.                             $aResult = _TCPIpToName("172.16." & $i & "." & $j)  
  48.                             If @error Then 
  49.                                 _log("->在线IP:172.16." & $i & "." & $j & " 已成功远程重启设备!")  
  50.                             Else 
  51.                                 _log("->在线IP:172.16." & $i & "." & $j & " 机器名:" & $aResult & " 已成功远程重启设备!")  
  52.                             EndIf  
  53.                             TCPShutdown()  
  54.                         EndIf  
  55.                     Else 
  56.                         $objItem.win32shutdown(5) ;关机  
  57.                         If Not @error Then 
  58.                             TCPStartup()  
  59.                             $aResult = _TCPIpToName("172.16." & $i & "." & $j)  
  60.                             If @error Then 
  61.                                 _log("→在线IP:172.16." & $i & "." & $j & " 成功远程关掉!")  
  62.                             Else 
  63.                                 _log("→在线IP:172.16." & $i & "." & $j & " 机器名:" & $aResult & " 成功远程关掉!")  
  64.                             EndIf  
  65.                             TCPShutdown()  
  66.                         EndIf  
  67.                     EndIf  
  68.                 Next 
  69.             EndIf  
  70.         EndIf  
  71.         Sleep(50)  
  72.     Next 
  73. Next 
  74.  
  75. Exit (MsgBox(262144 + 64, "完成-8秒后自动退出""可查看日志文件:" & $filelog & "查看执行情况", 5))  
  76.  
  77.  
  78. Func taskbarh()  
  79.     $pos = WinGetPos("[class:Shell_TrayWnd]")  
  80.     If IsArray($pos) Then 
  81.         Return $pos[3]  
  82.     EndIf  
  83. EndFunc   ;==>taskbarh  
  84.  
  85. Func Terminate()  
  86.     Exit (MsgBox(262144 + 16, "您按了 ESC 键?""点击 确定 强制退出!", 30))  
  87. EndFunc   ;==>Terminate  
  88.  
  89.  
  90. Func _log($varjiamiwr)  
  91.     _FileWriteLog($filelog, $varjiamiwr)  
  92. EndFunc   ;==>_log  
  93.  
  94.  
  95. ; This is my custom defined error handler  
  96. Func MyErrFunc()  
  97.     $LOALERROR = $oMyError.description  
  98.     If StringInStr($LOALERROR, "RPC"Or StringInStr($LOALERROR, "拒绝"Then 
  99.         _log("172.16." & $i & "." & $j & "可能为非计算机装备(打印服务器网关等?),故无法执行远程操作!错误代码:" & Hex($oMyError.number, 8) & " " & $LOALERROR)  
  100.     Else 
  101.         _log("172.16." & $i & "." & $j & " 因:" & $LOALERROR & " 错误代码:" & Hex($oMyError.number, 8) & " 无法执行远程操作!")  
  102.     EndIf  
  103. EndFunc   ;==>MyErrFunc  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
命令行改DNS存在一定的BUG,有时候不一定修改成功,论坛用户需要,我特别写了一个。 SETDNS.AU3 #Region ;**** 参数创建于 ACNWrapper_GUI **** #AutoIt3Wrapper_Icon=ip.ico #AutoIt3Wrapper_Outfile=SETDNS.exe #AutoIt3Wrapper_UseAnsi=y #AutoIt3Wrapper_Res_Comment=DNS修改工具 #AutoIt3Wrapper_Res_Description=DNS修改工具 #AutoIt3Wrapper_Res_Fileversion=1.0.0.2 #AutoIt3Wrapper_Res_LegalCopyright=TVZML作品 #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** 参数创建于 ACNWrapper_GUI **** Break(0) $g_Tray = "DNS修改工具" If WinExists($g_Tray) = 1 Then Exit AutoItWinSetTitle("DNS修改工具") $dir = (@ScriptDir & "\DNS.ini") $SectionNames = "配置信息" _setip() Exit Func _setip() ;导入IP配置 If Not FileExists($dir) Then MsgBox(16, "错误", "没有找到配置文件" & @CRLF & $dir, 3) Exit EndIf $colItems = "" $objWMIService = ObjGet("winmgmts:\\.\root\CIMV2") Dim $SetDns[2] $SetDns[0] = IniRead($dir, $SectionNames, "首选DNS", "") $SetDns[1] = IniRead($dir, $SectionNames, "备用DNS", "") TrayTip("", "正在设置DNS,请稍等...", 1) $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE", "WQL", 0x10 + 0x20) $colComputers = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem") If IsObj($colItems) Then For $objNetAdapter In $colItems $objNetAdapter.SetDNSServerSearchOrder($SetDns) Next EndIf Exit EndFunc ;==>_setip 配置文件样式 DNS.ini [配置信息] 首选DNS=220.189.127.106 备用DNS=220.189.127.108

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值