Skip to content

Service Monitor

Tiago Madeira edited this page Jan 28, 2025 · 17 revisions

Summary

ServiceMonitor.ps1 is a PowerShell script that monitors a specific Windows service and automatically restarts it if it stops. It uses WMI events for efficient, real-time monitoring and logs all actions to a file for auditing.

Disclaimer

Before running this script, you must replace all instances of <path> with the actual path where you saved the script. Failing to do so will result in errors. Open the script in a text editor like VS Code or PowerShell ISE. Locate the $serviceName variable and change its value to the exact name of the Windows service you wish to monitor. Before running this script, you must replace all instances of <path> with the actual path where you saved the script. Failing to do so will result in errors. Open the script in a text editor like VS Code or PowerShell ISE.

Locate the $serviceName variable and change its value to the exact name of the Windows service you wish to monitor.

Run script via PowerShell

  • Open a PowerShell console (administrator rights required).
  • Navigate to the folder containing the script
  • Call the script .\ServiceMonitor.ps1

Run script via Task Scheduler

1. Configure the Task:

  • Trigger: At system startup
  • Action: Run the PowerShell script -File C:\<Path>\ServiceMonitor.ps1

Settings:

  • Check "Run whether user is logged on or not"
  • Check "Run with highest privileges"

2. Keep the Infinite Loop:

  • The while ($true) loop ensures the script stays running in the background. Task Scheduler will treat it as a long-running task.

3. Hide the Console (Optional):

  • Add -WindowStyle Hidden to the PowerShell command to suppress the console window: -WindowStyle Hidden -File C:\<Path>\ServiceMonitor.ps1

Clone this wiki locally