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<br> New-SCOMManagementGroupConnection -ComputerName "localhost" -ErrorAction SilentlyContinue -ErrorVariable Err<br> New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ -ErrorAction SilentlyContinue -ErrorVariable Err | Out-Null<br> [xml]$xml = "<root></root>"<br> [string]$Query = ""<br> [string]$temp = ""<br> [string]$path = Split-Path -Parent $MyInvocation.MyCommand.Definition<br> [array]$AlertList = @()<br> [array]$groupObjects = @()<br> [array]$CriteriaList = Get-SCOMNotificationSubscription | where {$_.Enabled -eq "True"}<br> function Get-Criteria {<br>    Param([System.XML.XMLElement]$node, [string]$Criteria, [string]$AndOr)<br>    if($node.Name.StartsWith("Expression")) {<br>       if(![string]::IsNullOrEmpty($Criteria) -and $Criteria.EndsWith(")")) {<br>          $Criteria += " " + $AndOr + " "<br>       }<br>       $node = $node.FirstChild   <br>    }     if($node.Name.StartsWith("SimpleExpression")) {<br>       $temp = $node.ChildNodes[0].FirstChild.FirstChild.InnerText<br>       switch($temp) {<br>          "AlertId" {<br>             $Criteria += "(Id"<br>          }<br>          "AlertDescription" {<br>             $Criteria += "(Description"<br>          }<br>          "AlertName" {<br>             $Criteria += "(Name"<br>          }<br>          default {<br>             $Criteria += "(" + $temp<br>          }<br>       }<br>       $temp = $node.ChildNodes[1].FirstChild.InnerText<br>       switch($temp) {<br>          "Equal" {<br>             $Criteria += " = "<br>          }<br>          "NotEqual" {<br>             $Criteria += " != "<br>          }<br>          "Less" {<br>             $Criteria += " < "<br>          }<br>          "Greater" {<br>             $Criteria += " > "<br>          }<br>          "LessEqual" {<br>             $Criteria += " = "<br>          }<br>          default {<br>             $Criteria += " " + $temp + " "<br>          }<br>       }<br>       $Criteria += "'" + $node.ChildNodes[2].FirstChild.FirstChild.InnerText + "')"<br>    } elseif($node.Name.StartsWith("And") -or $node.Name.StartsWith("Or")) {<br>       $Criteria += "("<br>       foreach($elem in $node.ChildNodes) {<br>          $Criteria = Get-Criteria -node $elem -Criteria $Criteria -AndOr $node.Name<br>       }<br>       $Criteria += ")"<br>    }<br>    $Criteria<br> }<br> foreach($crit in $CriteriaList) {<br>    $xml = $crit.Configuration.Criteria<br>    $Query = Get-Criteria -node $xml.FirstChild -Criteria $Query -AndOr $xml.FirstChild.Name<br>    if($crit.Configuration.MonitoringClassIds.Count -gt 0) {<br>       $Query += " And (MonitoringClassId In ("<br>       foreach($mo in $crit.Configuration.MonitoringClassIds) {<br>          $Query += "'" + $mo + "',"<br>       }<br>       $Query = $Query.TrimEnd(",")<br>       $Query += "))"<br>    }<br>    if($crit.Configuration.MonitoringObjectGroupIds.Count -gt 0) { <br>       $temp = " And (MonitoringObjectId In ("<br>       foreach($groupId in $crit.Configuration.MonitoringObjectGroupIds) {<br>          $groups = Get-SCOMGroup -Id $groupId<br>          if($groups.Count -gt 0) {<br>             $groupObjects = $groups.GetRelatedMonitoringObjects()<br>             if($groupObjects.Count -gt 0) {<br>                foreach($obj in $groupObjects) {<br>                   $temp += "'" + $obj.Id + "',"<br>                }<br>             }<br>          }<br>       }<br>       $temp = $temp.TrimEnd(",")<br>       $temp += "))"<br>       if(!($temp.EndsWith(" And (MonitoringObjectId In ())"))) {<br>          $Query += $temp<br>          $temp = ""<br>       }<br>    }<br>    $AlertList += Get-SCOMAlert -Criteria $Query<br>    $Query = ""<br> }<br> if($AlertList.Count -gt 0) {<br>    $AlertList | Export-Csv "$path\SCOMAlertsByNotifications.csv" -NoTypeInformation<br> } else {<br>    Write-Error "It looks like there are no Alerts matching the criteria in the database or there may be another problem."<br> }