51Testing软件测试论坛

标题: VBS 监测未响应程序脚本 [打印本页]

作者: wuxue107    时间: 2013-7-13 10:50
标题: VBS 监测未响应程序脚本
之前帮一个玩游戏的朋友写的一个小脚本。检测未相应程序并关闭的

  1. On Error Resume Next
  2. If LCase(Right(WScript.FullName,11)) = "wscript.exe" Then
  3.         CreateObject("Wscript.Shell").Run "CScript "&Chr(34)&WScript.ScriptFullName&Chr(34)
  4.         WScript.quit
  5. End If

  6. Timeout = 60                        '多少秒数关闭不响应程序
  7. Times = 6                                '每多少秒检测一次,时间误差为一个检测周期

  8. '################  白名单   ############## 字母用小写
  9. blist = array("system idle process","svchost.exe","conhost.exe", _
  10. "csrss.exe","explorer.exe","lsass.exe","smss.exe","lsm.exe","spoolsv.exe", _
  11. "system","wininit.exe","winlogon.exe","services.exe","taskhost.exe","dwm.exe")

  12. Set objshell = CreateObject("wscript.shell")
  13. set dc = CreateObject("Scripting.Dictionary")
  14. Do
  15.         set out = objshell.exec("cmd /c tasklist /v|find "" Not Responding """).stdout
  16.         do while Not out.AtEndOfLine
  17.                 m=out.ReadLine
  18.                 name = LCase(Trim(Left(m,23)))
  19.                 pid = Trim(Mid(m,27,8))
  20.                 b=false
  21.                 For Each n In blist
  22.                         If name = n Then b = true:Exit For
  23.                 Next
  24.                
  25.                 If Not b Then
  26.                         'WScript.Echo "At: " &Date & " " & time & ":TASKKILL  /F /PID " & pid
  27.                         If Not dc.Exists("n" & pid) Then
  28.                                 dc.Add "n" & pid,Times
  29.                         Else
  30.                                 dc.Item("n" & pid) = dc.Item("n" & pid)+Times
  31.                         End If
  32.                         If Time-lt>Times+20 Then dc.Item("n" & pid)=Times
  33.                         lt= Time

  34.                         WScript.Echo "At: " & FormatDateTime(Now,2) & " ProcessName:" & Left(m,23) & "  PID:" & Mid(m,27,8) & "  Time out: " & String(6-Len(dc.Item("n" & pid))," ") & dc.Item("n" & pid) & " Second!"
  35.                         If dc.Item("n" & pid)>=Timeout Then
  36.                                 objshell.Run "TASKKILL  /F /PID " & pid ,0
  37.                                 WScript.Echo "At: " & FormatDateTime(Now,2) &  " ProcessName:" & Left(m,23) & "  PID:" & Mid(m,27,8) & "  Execute: " & "TASKKILL  /F /PID " & pid
  38.                         End if
  39.                 End if
  40.         Loop
  41.        
  42.         WScript.Sleep Times * 1000
  43. Loop While true

复制代码





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2