For which Alerts did I get a Notification in SC Operations Manager 2012?


Have you ever asked yourself, which Alerts that are currently in the OperationsManager database match your configured Criteria from your Subscriptions? Well, I have been asked this and so the easiest way was to create a PS Script which uses the Configuration of every Subscription and exports all the Alerts it finds in the database which match the Criteria into a CSV file which it saves in the same folder in which the Script will be executed from.

I did try it with a lot of Subscriptions with very different Criteria, but I did not test every possible scenario, so in case that the Script will throw some Exception in some of its Batch-Runs, then just drop me a comment here so I can have a look at that specific scenario.

The Script needs to be executed on an OM Management Server with the OM Console and PowerShell modules installed. It will import the OM PS Modules automatically so you don’t have to worry about running it under an OM PowerShell.

Just remember that the script does need to be saved in a PS1 file and this needs to get executed. If you just paste the Script in a PS Window, then it will fail to create the CSV because it won’t actually be able to get its directory. Feel free to adjust the Script to your needs – maybe you also need to export the E-Mail Addresses for each of your Subscriptions that go along with the Alerts? That could be just one of the additions you might need.

 

 Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue -ErrorVariable Err
New-SCOMManagementGroupConnection -ComputerName "localhost" -ErrorAction SilentlyContinue -ErrorVariable Err
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ -ErrorAction SilentlyContinue -ErrorVariable Err | Out-Null
[xml]$xml = "<root></root>"
[string]$Query = ""
[string]$temp = ""
[string]$path = Split-Path -Parent $MyInvocation.MyCommand.Definition
[array]$AlertList = @()
[array]$groupObjects = @()
[array]$CriteriaList = Get-SCOMNotificationSubscription | where {$_.Enabled -eq "True"}
function Get-Criteria {
   Param([System.XML.XMLElement]$node, [string]$Criteria, [string]$AndOr)
   if($node.Name.StartsWith("Expression")) {
      if(![string]::IsNullOrEmpty($Criteria) -and $Criteria.EndsWith(")")) {
         $Criteria += " " + $AndOr + " "
      }
      $node = $node.FirstChild   
   }

   if($node.Name.StartsWith("SimpleExpression")) {
      $temp = $node.ChildNodes[0].FirstChild.FirstChild.InnerText
      switch($temp) {
         "AlertId" {
            $Criteria += "(Id"
         }
         "AlertDescription" {
            $Criteria += "(Description"
         }
         "AlertName" {
            $Criteria += "(Name"
         }
         default {
            $Criteria += "(" + $temp
         }
      }
      $temp = $node.ChildNodes[1].FirstChild.InnerText
      switch($temp) {
         "Equal" {
            $Criteria += " = "
         }
         "NotEqual" {
            $Criteria += " != "
         }
         "Less" {
            $Criteria += " < "
         }
         "Greater" {
            $Criteria += " > "
         }
         "LessEqual" {
            $Criteria += " = "
         }
         default {
            $Criteria += " " + $temp + " "
         }
      }
      $Criteria += "'" + $node.ChildNodes[2].FirstChild.FirstChild.InnerText + "')"
   } elseif($node.Name.StartsWith("And") -or $node.Name.StartsWith("Or")) {
      $Criteria += "("
      foreach($elem in $node.ChildNodes) {
         $Criteria = Get-Criteria -node $elem -Criteria $Criteria -AndOr $node.Name
      }
      $Criteria += ")"
   }
   $Criteria
}
foreach($crit in $CriteriaList) {
   $xml = $crit.Configuration.Criteria
   $Query = Get-Criteria -node $xml.FirstChild -Criteria $Query -AndOr $xml.FirstChild.Name
   if($crit.Configuration.MonitoringClassIds.Count -gt 0) {
      $Query += " And (MonitoringClassId In ("
      foreach($mo in $crit.Configuration.MonitoringClassIds) {
         $Query += "'" + $mo + "',"
      }
      $Query = $Query.TrimEnd(",")
      $Query += "))"
   }
   if($crit.Configuration.MonitoringObjectGroupIds.Count -gt 0) {
      $temp = " And (MonitoringObjectId In ("
      foreach($groupId in $crit.Configuration.MonitoringObjectGroupIds) {
         $groups = Get-SCOMGroup -Id $groupId
         if($groups.Count -gt 0) {
            $groupObjects = $groups.GetRelatedMonitoringObjects()
            if($groupObjects.Count -gt 0) {
               foreach($obj in $groupObjects) {
                  $temp += "'" + $obj.Id + "',"
               }
            }
         }
      }
      $temp = $temp.TrimEnd(",")
      $temp += "))"
      if(!($temp.EndsWith(" And (MonitoringObjectId In ())"))) {
         $Query += $temp
         $temp = ""
      }
   }
   $AlertList += Get-SCOMAlert -Criteria $Query
   $Query = ""
}
if($AlertList.Count -gt 0) {
   $AlertList | Export-Csv "$path\SCOMAlertsByNotifications.csv" -NoTypeInformation
} else {
   Write-Error "It looks like there are no Alerts matching the criteria in the database or there may be another problem."
}

 

 


Comments (0)

Skip to main content