Advertisement
Guest User

Untitled

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