PowerShell script for monitoring Windows Service memory utilization

Recently, we were facing issues with one of our windows service, its memory utilization keep on increasing over time i.e. in goes for few MBs to 2-3 GB in span of  2-3 months of continuous running. For a quick fix (workaround) to address this issue,

  1. We created a PowerShelll script that:
    1. Check Windows Service for its current memory utilization
    2. Restart Windows Service if its memory usage exceeds specified Threshold value
  2. Create a Task Scheduler that runs PowerShell script on daily basis

In this post, I will be sharing how we can use a simple PowerShell script for monitoring Windows Service memory utilization.

Step 1: Check Windows Service for its current memory utilization

Using Windows Management Interface (WMI) 

$ServiceName = "MySampleService"
$Service=Get-WmiObject Win32_Service -Filter "name = '$ServiceName'"
$ProcessID = $Service.ProcessID
$ProcessMem = Get-WmiObject Win32_Process -Filter "ProcessId = '$ProcessID'"
$MemSizeInMB = $ProcessMem.WS/1MB
write-host $ProcessMem.WS

OR

Using Common Information Model (CIM) – Recommended

$ServiceName = "MySampleService"
$Service=Get-CIMInstance Win32_Service -Filter "name = '$ServiceName'"
$ProcessID = $Service.ProcessID
$ProcessMem = Get-CIMInstance Win32_Process -Filter "ProcessId = '$ProcessID'"
$MemSizeInMB = $ProcessMem.WS/1MB
write-host $ProcessMem.WS

Here: $ServiceName = Set it to name of the service you want to monitor.

Eg.:

For “Task Scheduler” service, Service Name is Schedule

Task Scheduler

Get-WmiObject Win32_Service -Filter "name = 'Schedule'"

Get Process ID

Get-WmiObject Win32_Process -Filter "ProcessId = '1136'"

Process Info DetailsGet Process Info

Step 2: Restart Windows Service if its memory usage exceeds specified Threshold value

$MemoryThresholdInMB = 2048

If ($MemSizeInMB -gt $MemoryThresholdInMB) {
  write-host "$ServiceName : $MemSizeInMB"
  Restart-Service -Name $ServiceName
  write-host "$ServiceName Restarted"
}

Here:

$MemoryThresholdInMB = Memory Threshold in MB

$MemSizeInMB = Service Current Memory Usage (WorkingSet) that we get from Step 1

Step 3: Full sample Powershell script

Windows-Service-Monitoring.ps1 content:

Using Windows Management Interface (WMI) 

$ServiceName = "MySampleService"
$MemoryThresholdInMB = 2048

$Service=Get-WmiObject Win32_Service -Filter "name = '$ServiceName'"
$ProcessID = $Service.ProcessID
$ProcessMem = Get-WmiObject Win32_Process -Filter "ProcessId = '$ProcessID'"
$MemSizeInMB = $ProcessMem.WS/1MB

write-host $ProcessMem.WS
write-host "$MemSizeInMB : $MemoryThresholdInMB"

If ($MemSizeInMB -gt $MemoryThresholdInMB) {
  write-host "$ServiceName : $MemSizeInMB"
  Restart-Service -Name $ServiceName
  write-host "$ServiceName Restarted"
}

OR

Using Common Information Model (CIM) – Recommended

$ServiceName = "MySampleService"
$MemoryThresholdInMB = 2048

$Service=Get-CIMInstance Win32_Service -Filter "name = '$ServiceName'"
$ProcessID = $Service.ProcessID
$ProcessMem = Get-CIMInstance Win32_Process -Filter "ProcessId = '$ProcessID'"
$MemSizeInMB = $ProcessMem.WS/1MB

write-host $ProcessMem.WS
write-host "$MemSizeInMB : $MemoryThresholdInMB"

If ($MemSizeInMB -gt $MemoryThresholdInMB) {
  write-host "$ServiceName : $MemSizeInMB"
  Restart-Service -Name $ServiceName
  write-host "$ServiceName Restarted"
}

Set $ServiceName & $MemoryThresholdInMB as per your requirements.

Step 3: Use “Windows Task Scheduler” for executing the PowerShell script on daily at a given time.

Windows Service Performance Monitor

Here:

Program: powershell

Arguments: -File C:\Windows-Service-Monitoring.ps1

That’s It !!!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *