SHOW:
|
|
- or go back to the newest paste.
1 | 'I got this code from this post: | |
2 | 'http://www.petri.co.il/forums/showthread.php?t=53689 | |
3 | ||
4 | option explicit | |
5 | ||
6 | Const PROCESS = "EWBIC.exe" | |
7 | Const MEM_LIMIT_KB = 511999 | |
8 | Const SERVICE_NAME = "IC Service" '! make sure to use the service name here not the displayname of the service | |
9 | ||
10 | Dim objSWbemObject, strComputer | |
11 | Dim objSWbemServices, objSWbemEventSource | |
12 | Dim objProcess, iPID, iMem | |
13 | ||
14 | strComputer = "." | |
15 | ||
16 | Set objSWbemServices = GetObject("winmgmts:" _ | |
17 | & "{impersonationLevel=impersonate}!\\" _ | |
18 | & strComputer & "\root\cimv2") | |
19 | ||
20 | ' Enable the SeDebugPrivilege privilege to terminate | |
21 | ' processes running under a different security context. | |
22 | objSWbemServices.Security_.ImpersonationLevel = 3 | |
23 | objSWbemServices.Security_.privileges. _ | |
24 | addasstring "SeDebugPrivilege", True | |
25 | ||
26 | Set objSWbemEventSource = objSWbemServices.ExecNotificationQuery( _ | |
27 | "SELECT * FROM __InstanceModificationEvent WITHIN 60 Where (" & _ | |
28 | "TargetInstance ISA 'Win32_Process' AND " & _ | |
29 | "TargetInstance.Name='" & PROCESS & "' AND " & _ | |
30 | "TargetInstance.WorkingSetSize > " & MEM_LIMIT_KB *1024 & ")") | |
31 | ||
32 | On Error Resume Next | |
33 | Do: Set objSWbemObject = objSWbemEventSource.NextEvent() | |
34 | ||
35 | Select Case objSWbemObject.TargetInstance.Path_.Class | |
36 | ||
37 | Case "Win32_Process" | |
38 | wscript.sleep 10000 ' wait 10 seconds then re-check mem usage by this process | |
39 | ||
40 | For Each objProcess in getobject("winmgmts://").instancesof(_ | |
41 | "Win32_Process Where Name = '" & PROCESS & "' AND WorkingSetSize > " & MEM_LIMIT_KB *1024) | |
42 | iPID = objProcess.Processid | |
43 | iMem = objProcess.WorkingSetSize /1024 | |
44 | ||
45 | objProcess.Terminate() ' optional | |
46 | ||
47 | call notifyMe (iPID, iMem) | |
48 | ||
49 | ' stop/start service (do not use its displayname here) | |
50 | With objSWbemServices.Get("Win32_Service.Name='" & SERVICE_NAME & "'") | |
51 | .StopService() | |
52 | wscript.sleep 10000 ' wait 10 seconds | |
53 | .StartService() | |
54 | wscript.sleep 10000 ' wait 10 seconds | |
55 | End With | |
56 | Next | |
57 | ||
58 | End Select | |
59 | ||
60 | Loop | |
61 | ||
62 | Sub notifyMe (PID, Mem) | |
63 | ||
64 | With CreateObject("WScript.Shell") | |
65 | .popup PROCESS & " [ID " & PID & "] terminated " _ | |
66 | & "because its memory usage was: " & Mem & " kb", _ | |
67 | 5, strComputer & " Event", 0+64 | |
68 | End With | |
69 | ||
70 | End Sub |