Hi, I have this small script that should kill dllhost (yes i know this is not an elegant solution and shouldn't be done this way), but its only for a few months time before this server is retired. So we are using windows 2000 and we need to kill dllhost when it exceeds certain amount of memory usage, the script calculates the memory correctly and retrieves all the process as well with their PID, however they are not being killed, what should i change??
thank you!
Code:
Option Explicit
Dim objWMIService, objProcess, objFSO, objTextFile
dim colProcess,colServices, colItems
dim objItem, objService, objLoc
Dim strComputer, strProcessKill, strProcessMem
Dim memLimit, processMem
Dim mustKill
strComputer = "."
strProcessKill = "'DLLHOST.EXE'"
strProcessMem = "'DLLHOST'"
memLimit = 10000
mustKill = 0
Set objLoc = createobject("wbemscripting.swbemlocator")
objLoc.Security_.privileges.addasstring "sedebugprivilege", true
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = " & strProcessKill )
dim intRC
For Each objProcess in colProcess
msgbox "El pid es..." & ObjProcess.ProcessId
processMem = objProcess.WorkingSetSize/1024
msgbox "El size es " & processMem
If processMem >= memLimit Then
MsgBox "pasa de " & memLimit & " Kb"
mustKill = 1
Else
MsgBox "es menor " & memLimit & " Kb"
End If
if mustKill = 1 then
msgbox "voy a matarlo"
objProcess.Terminate()
if intRC = 0 Then
msgbox "Successfully killed process."
else
msgbox "Could not kill process. Error code: " & intRC
end if
end if
Next
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\informe_dllhost.txt", ForAppending, True)
if mustKill = 1 then
objTextFile.WriteLine(now & vbnewline & " El proceso se ha matado con " & processMem & " Kb" & vbnewline & vbNewLine)
end if
objTextFile.Close
WScript.Quit