Understanding SCOM Subscriptions and creating new ones using PS 3.0

Start by creating a template subscription:

Name
Template Subscription

Description
Description

Criteria
Notify on all alerts where
raised by any instance in a SQL Server 2008 Computers;SQL Server 2012 Computers;Fehse – Virtual Disks Group;SQL Computers;teste;Fehse – Virtual Machines Group group
and  created by Auto_Maintenance_Mode rules or monitors (e.g., sources)
and  of a Critical severity
and  with New (0) resolution state 

Subscribers
FEHSECORP\administrator 

Channels
SMTP Channel (local scom) 

 

Let’s take a look at the Subscription itself:

Get-SCOMNotificationSubscription -DisplayName “Template Subscription”

image

So, where is all that we have configured before. Let’s start by the subscribers.

First assign the subscription to a variable:

$sub=Get-SCOMNotificationSubscription -DisplayName “Template Subscription”

Then let’s take a peek at  $sub.ToRecipients

image

Simple enough. Let’s see how it plays later, to actually add subscribers.

Let’s take a look at the Actions:

image

It seems you have a copy of the Channel template in there.

Now, the juicy and much expected configuration:

image

Note the MonitoringObjectGroupIds. If you check that guid using get-scomgroup you will see:

image

Which makes sense. Also note the criteria:

image

This accounts for this part of the Criteria:

and  created by Auto_Maintenance_Mode rules or monitors (e.g., sources)
and  of a Critical severity
and  with New (0) resolution state
 

You can basically save those for later use, if they are the same:

$criteria=$sub.Configuration.Criteria

You can keep copies of the configuration chunks (XML) to add to your script.

Speaking of which, after analyzing and understanding how the subscription creation process works, I have put together a script that will leverage PowerShell 3.0 gui prompts to ask you for the required info.

I’m assuming you have created the subscribers beforehand.

Here’s the commented version of the script.

Import-Module OperationsManager
#$defaultcriteria is a basic standard, which include New alerts critical alerts (ResolutionState=0 and Severity=2)
$defaultcriteria=@"
<And xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Expression>
    <SimpleExpression>
      <ValueExpression>
        <Property>Severity</Property>
      </ValueExpression>
      <Operator>Equal</Operator>
      <ValueExpression>
        <Value>2</Value>
      </ValueExpression>
    </SimpleExpression>
  </Expression>
  <Expression>
    <SimpleExpression>
      <ValueExpression>
        <Property>ResolutionState</Property>
      </ValueExpression>
      <Operator>Equal</Operator>
      <ValueExpression>
        <Value>0</Value>
      </ValueExpression>
    </SimpleExpression>
  </Expression>
"@
#$ruleIdCriteriaTemplate is a piece of XML that is used to add Monitoring rules to the notification.
$ruleIdCriteriaTemplate=@"
<Expression>
  <SimpleExpression>
    <ValueExpression>
      <Property>RuleId</Property>
    </ValueExpression>
    <Operator>Equal</Operator>
    <ValueExpression>
      <Value>##RuleId##</Value>
    </ValueExpression>
  </SimpleExpression>
</Expression>
"@
$newsubname=Read-Host "New Subscription Name" #asks for the new notification name
$Channel=Get-SCOMNotificationChannel -DisplayName "SMTP Channel (local scom)" #uses this channel. Change depending on the case. Or make it also a prompt.
#the line below queries SCOM's subscriber list and shows a grid to be picked from.
$Recipients=Get-SCOMNotificationSubscriber | Out-GridView -PassThru

$newcriteria = $defaultcriteria #initializes the $newcriteria to be used in the notification. 
#Displays a full set of SCOM rules to be picked from. If not need, can be skipped.
$selectedrules=get-scomrule | where {$_.DisplayName -ne $null } | select DisplayName, Id |Sort-Object DisplayName | Out-GridView -PassThru
#if more than one rule was selected, adds an 'Or' to the expression
if ($selectedrules.Count -gt 1) { $newcriteria+="<Expression><Or>" }
foreach ($selRul in $selectedrules)
{
    $tempcriteria=$ruleIdCriteriaTemplate
    $tempcriteria=$tempcriteria.Replace("##RuleId##", $selRul.Id)
    $newcriteria+=$tempcriteria
}
#if more than one rule was selected, closes the 'Or' expression
if ($selectedrules.Count -gt 1) {    $newcriteria+="</Or></Expression>" }
$newcriteria+="</And>" #closes the And expression
#$newcriteria
#shows a list of SCOM groups to narrow down the notification
$selectedgroups=get-scomgroup | select DisplayName, Id | Sort-Object DisplayName | Out-GridView -PassThru

#creates the new subscription
try
{
    $newSub=Add-SCOMNotificationSubscription -Name $newsubname -Channel $Channel -Subscriber $Recipients -Criteria $newcriteria
    #in order to add groups and classes to the criteria, you need to do it after the subscription has been created.
    if ($newSub -ne $null -and $selectedgroups -ne $null)
    {
        foreach($group in $selectedgroups)
        {
                #adds each selected group
                $newsub.Configuration.MonitoringObjectGroupIds.Add($group.Id)
        }
        $newSub.Update() #Updates the subscription
    }
}
catch 
{
    #shows an error if something goes wrong with the creation process.
    write-host "Error creating subscription. $error"
}


Hope this helps you understanding the notifications and how to leverage PowerShell to create them. Find a copy of the script here.