We had a rogue spool service on a terminal server we needed to monitor memory usage of, I used the shell command tasklist to output the process to a log file, then used a VBScript to intepret the log results and restart the service if its required.

Its a dirty script, but its functional.

KScript 1
Script Name: Print Spooler memory monitor 1
Script Description: Monitors memory usage for spoolsv.exe
Threshold 100mb

IF True
THEN
Get Variable
Parameter 1 : 10
Parameter 2 :
Parameter 3 : AgentTemp
OS Type : 0
Write File
Parameter 1 : #AgentTemp#\procmemcheck.vbs
Parameter 2 : VSASharedFiles\procmemcheck.vbs
OS Type : 0
Execute Shell Command
Parameter 1 : tasklist /FO list /FI "IMAGENAME eq spoolsv.exe" | Find "Mem Usage" >> #AgentTemp#\spoolmem.log
Parameter 2 : 1
OS Type : 0
Execute Shell Command
Parameter 1 : cscript.exe #AgentTemp#\procmemcheck.vbs "#AgentTemp#\spoolmem.log" "Spooler" "100"
Parameter 2 : 1
OS Type : 0
Execute Script
Parameter 1 : Print Spooler Memory monitor 2 (NOTE: Script reference is NOT imported. Correct manually in script editor.
Parameter 2 :
Parameter 3 : 0
OS Type : 0
ELSE



KScript 2
Script Name: Print Spooler Memory monitor 2
Script Description: Checks if memory has breached threshold and notifies service desk

IF Test File
Parameter 1 : c:\temp\procmemcheck.log
Exists :true
THEN
Get Variable
Parameter 1 : 6
Parameter 2 :
Parameter 3 : Machine
OS Type : 0
Get Variable
Parameter 1 : 10
Parameter 2 :
Parameter 3 : AgentTemp
OS Type : 0
Get Variable
Parameter 1 : 1
Parameter 2 : #AgentTemp#\procmemcheck.log
Parameter 3 : LogData
OS Type : 0
Send Email
Parameter 1 : help@mycompany.com
Parameter 2 : #Machine# Alert: Spool Service
Parameter 3 : #LogData#
OS Type : 0
Delete File
Parameter 1 : #AgentTemp#\procmemcheck.log
OS Type : 0
Write Script Log Entry
Parameter 1 : #LogData#
OS Type : 0
ELSE



VBScript
'Name: procmemcheck
'Script written by Andrew Ramsay 2009
'Date 22/09/2009


'declare variables
dim strLogFilePath, intMemThresh, strSvcName 'script arguments
dim strStep1, strStep2, strStep3 'string parsing place holders
dim objFSO, objTextFile 'Scripting.FileSystem Objects
dim objWMI, objService, strComputer 'WMI Query Objects
dim memUsed 'output

'setup variables
strLogFilePath = WScript.Arguments.Item(0) '1st arg, tasklist log path
strSvcName = WScript.Arguments.Item(1) '2nd arg, service monitored
intMemThresh = CInt(WScript.Arguments.Item(2)) '3rd arg, Threshold limit
strComputer = "."

'read log file
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
(strLogFilePath, 1)
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.Readline
Loop
objTextFile.Close

'Parse log output into value in megabytes
strStep1 = replace(strLine,"Mem Usage:","")
strStep2 = replace(strStep1,",","")
strStep3 = replace(strStep2,"K","")

If isNumeric(strStep3) Then
memUsed = round(strStep3 / 1024)
else Wscript.quit
end if

'Restart Service if it is over threshold
if memUsed > intMemThresh then
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'")

objService.StopService
objService.StartService

'Write log file for Kaseya
set objTextFile = objFSO.CreateTextFile("c:\temp\procmemcheck.log",true)
objTextFile.WriteLine("Memory limit for " & strSvcName & " exceeded threshold: Service Restarted")
objTextFile.Close

else wscript.quit
end if


Legacy Forum Name: Script to monitor memory usage of a Process/Service,
Legacy Posted By Username: andrewra