Friday, July 31, 2009

PowerShell: Changing the Resolution State

Suppose you have multiple Alerts coming in and want to change the Resolution State automatically from New to something like DBA for instance.

How to go about it? There are multiple ways to address it. I will describe one workable solution for it.

Be aware that this process has to be scheduled on the RMS and causes some overhead. However, when the RMS is reasonably  dimensioned it shouldn’t be any problem.

  1. Create the Resolution State (when not present) in the Administration Pane > Settings > Alerts. As an example I create the Resolution State DBA with ID 40:
    image 
  2. Also in this example I target the Alerts based on their name. Here I chose the Alerts with the name ‘Script or Executable Failed to run’. This name will be used in the PS script. 
    image
  3. Now I start in good old Notepad with the PS script being used to change the Resolution State of this Alert from New to DBA. Since I will use the regular Task Scheduler of the Windows Server I will run it from the command prompt. As we all know OpsMgr adds certain PS extensions to the regular PS. And we need those in order to get the script running. So this script has to load these extensions as well. Secondly, the script needs to connect to the RMS. There are two ways to go about it: hardcode it in the script or use a parameter. I prefer the latter since it makes the script more transferable to other Management Groups as well. This is the first section of the script just doing these actions:

    param ($rootMS)

    #Initializing the Ops Mgr 2007 Powershell provider
         add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin ;
         set-location "OperationsManagerMonitoring::" -ErrorVariable errSnapin ;
         new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin ;
         set-location $rootMS -ErrorVariable errSnapin ;


  4. Now I need the second part of the script. PS has to look for new alerts with the name ‘Script or Executable Failed to run’. When found the status has to be changed to ‘DBA’. The script also needs a loop in order to handle multiple Alerts with the same status and name:

    $Alerts = get-alert -criteria 'Name = ''Script : Failed to login''' | Where {$_.ResolutionState -eq 0}
    foreach($Alert in $Alerts)
    {$Alert.ResolutionState = 40
    $Alert.Update("Resolution State Changed")}


  5. I put these two parts together in one script and save it as a .ps1 file (ChangeRState_01.ps1) on the RMS. From the cmd-prompt I test it. Since I have parameterized the name of the RMS I have to use the FQDN of the RMS as well:
    Syntax: Powershell <location and name PS script> –rootMS <FQDN RMS>

    In my case it becomes:

    Powershell c:\ChangeRState_01.ps1 –rootMS srv01.systemcenter.org 
    image
  6. Script has run successfully:
    image

Now this script has to be put in the right folder and scheduled on the server. I will not explain that since I presume one knows how to go about it. However two more advises:

  1. Do not run this script to often (every 15 minutes should be sufficient).
  2. Run this scheduled script with an account that has sufficient authorizations (but not more!) within OpsMgr. Operator Permissions should do the trick.  

Example script can be downloaded here (ChangeResState.ps1). You know the drill: it is provided as is, no warranties, test it first in a test environment before putting it into production…