Use Start-Sleep to Delay PowerShell Script Execution in Operations Manager Recovery Task

I had an issue where I needed a monitor in Operations Manager to start a service when it crashed.  Which is easy enough to do (you can find all kinds of examples on it), except that in my case the service wasn’t just stopping immediately.  It is a custom homegrown application that detected a dropped connection, wrote an event to the event log (which Operations Manager was watching), and then killed the service.  The problem is that it would write the event, and then attempt to stop the service, which took several seconds to stop.  In that interval Operations Manager would pick up the event, try to start the service, determine that it wasn’t stopped, and fail.  So, I needed to figure out a way to “delay” the recovery task.  I couldn’t find a way to do this natively in the recovery task, so I did it in PowerShell.  The PowerShell “script” itself was a simple two lines.

Start-Sleep -Seconds 30
Start-Service -Name "ServiceName"

This forced the script to wait 30 seconds to run, and then started the service.  Here is what the recovery task in SCOM looks like.

Capture

Leave a Reply