Adding A Basic PowerShell recovery to a Monitor

This blog post is in response to a comment from Stephen.  He tried to use my blog post on Adding a Restart Recovery to a Service Monitor  in order to restart the winmgmt service.  The problem is that this script will not work on winmgmt because it is dependent on WMI.  Stephen has worked up his own PowerShell script to do the recovery, available on his blog.  I will walk through how to add this recovery to a monitor.

The first thing you will notice if you try to do this is that you cannot accomplish this through the Ops Console, you need to use the Authoring Console.  This is because the Ops Console only makes the VBScript module available for script based recoveries.  In the Authoring Console we can use the PowerShell Write Action module for this.  So, without further Ado, lets get this party started!

Open up the Authoring Console and make a new MP for this override (or export an existing override MP from your environment and add this recovery to it).

image

 

 

image

Make sure that the MP you imported or the MP you created has a reference to the sealed MP that has the monitor you would like to create the recovery for (if the monitor is in the same MP you can ignore this).

image

Now we can go ahead and make a new Recovery.  Go to Health Model –> Tasks –> Recoveries and say create new custom recovery

image

Set the target to the same target as the Monitor you are trying to add the recovery for (in my example I will be adding a recovery to a monitor that is targeting my GMI.PSMonitoring.Base class – this is a monitor that runs a PowerShell workflow and only fires on servers with PowerShell so I know using a PowerShell recovery will be ok).

image

 

Choose the monitor you want the recovery to trigger off of and choose the Health State you want it to trigger off of.  Note default timeout of 5 minutes is probably a good default

image

 

Next we create a new Action.  This is where will will input the script.  We want to use a PowerShellWriteAction module.

image

Now we can go ahead and edit it

image

The following is how the XML should look in order to run Stephen’s service recovery.

  • We put the Name of the script in the <ScriptName> block
  • Add the actual script to the <ScriptBody> Block (Note I also added a <![CDATA[ ]]> block surrounding the script which tells SCOM not to parse the script).
  • Add a <Parameters> Block immediately after the <ScriptBody> block and before the <TimeOut> block
    • Each <Parameter> block in the <Parameters> has to sections, a <Name> block and a <Value> block.  Name refers to the Parameter name that the script expects to have passed, in this case svcName and the Value block represents what you want to pass as the value for that parameter, in this case winmgmt
  • Set the <TimeOut> block to 300 which mean 300 seconds or 5 minutes (matches with the monitor timeout

<ScriptName>ServiceRecycle.PS1</ScriptName>
  <ScriptBody><![CDATA[
# RestartService.ps1
# Author: STL
# Created: 6/22/2010
# Purpose: Restart service and dependencies
PARAM($svcName)
# Get dependent services
$depSvcs = Get-Service -name $svcName -dependentservices | Where-Object {$_.Status -eq “Running”} |Select -Property Name
# Check to see if dependent services are started
if ($depSvc -ne $null) {
    # Stop dependencies
    foreach ($depSvc in $depSvcs)
    {   
        Stop-Service $depSvc.Name
        do
        {
            $service = Get-Service -name $depSvc.Name | Select -Property Status
            Start-Sleep -seconds 1
        }
        until ($service.Status -eq “Stopped”)
    }
}
# Restart service
Restart-Service $svcName -force
do
{
    $service = Get-Service -name $svcName | Select -Property Status
    Start-Sleep -seconds 1
}
until ($service.Status -eq “Running”)
$depSvcs = Get-Service -name $svcName -dependentservices |Select -Property Name
# We check for Auto start flag on dependent services and start them even if they were stopped before
foreach ($depSvc in $depSvcs)
{
    $startMode = gwmi win32_service -filter “NAME = ‘$($depSvc.Name)'” | Select -Property StartMode
    if ($startMode.StartMode -eq “Auto”) {
        Start-Service $depSvc.Name
        do
        {
            $service = Get-Service -name $depSvc.Name | Select -Property Status
            Start-Sleep -seconds 1
        }
        until ($service.Status -eq “Running”)
    }
}
]]>
  </ScriptBody>
  <Parameters>
    <Parameter>
      <Name>svcName</Name>
      <Value>winmgmt</Value>
    </Parameter>
  </Parameters>
  <TimeoutSeconds>300</TimeoutSeconds>

After that your configuration should look something like

image

and now your recovery is setup!  Save and import this MP into your environment after testing and have fun!

image

Advertisements
This entry was posted in Management Pack Authoring, Scripting and tagged , , . Bookmark the permalink.

5 Responses to Adding A Basic PowerShell recovery to a Monitor

  1. Stephen says:

    Appreciate the post! … and thanks for getting back so quickly. I’ll give it a shot and let you know.

  2. Stephen says:

    Finally tried it out. Worked like a champ! Thanks again!

  3. Anissiar says:

    It’s best to take part in a contest for probably the greatest blogs on the web. I will advocate this website!

    • Tyson says:

      The above is a spam comment. I get them EVERY DAY OF MY LIFE. Drives me crazy.
      Also, thanks for the post. I was hoping to find info about configuring a Powershell diagnostic task but this is also helpful.

  4. Pingback: Automation Scripting PowerShell: Servicios, SSRS, etc « El TecnoBaúl de Kiquenet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s