OpsMgr 2007: Creating a subscription for future occurrences of an alert

Ever want an easy way to have an email notification sent whenever a specific alert occurs?  Back in the MOM 2005 days this was pretty simple but today with System Center Operations Manager 2007, while the functionality is still there, the UI is somewhat limited in this regard.  As a proof of concept, I created a PowerShell script and MP that when used together will allow you to quickly and easily subscribe to future occurrences of an alert in the OpsMgr console.

Below is the text for the Powershell script and the MP.  I don't have a good way to attach files here so you'll have to copy and past and create your own.  For the PowerShell script, use the text below to create "CreateSub.ps1".  For the MP, use the text specified below to create "Create.A.Subscription.By.RightClicking.an.Alert.xml".  Note that you will need to edit two things in the "CreateSub.ps1" script to make it work in your environment:

$RMS = “server.domain.com” This needs to be your RMS server address.
$NotificationRecipientName = "user@domain.com" The email address you want notifications to be sent to.

To implement this you need the CreateSub.ps1 file to be placed in C:\scripts on the RMS and then need to import the MP.  Also make sure that PowerShell and the Command Shell are installed on the RMS server.  Next create the Notification Recipient and ensure that the name of the recipient matches the email address.  Once you do this, when you select an Alert from the Monitoring pane in the OpsMgr 2007 console, you will see an Alert task in the Action pane called “Create a Subscription For This Problem”.  When you select this task it will create a notification for that problem (filtering on problem ID) that will send emails to the email address in the script.

It may take a while to display the subscription, but if you shut down the OpsMgr console and open it again, hey presto - there it is.  It is also worth pointing out that I did this all on one box, the RMS, and haven’t attempted this from a separate machine running the OpsMgr 2007 console.

CreateSub.ps1

## check for parameters
param(
[string]$AlertID
)

write-host $alertID

$RMS = "server.domain.com"
$NotificationRecipientName = "administrator@domain.com"

## prepare OpsMgr shell
if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
{
Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-Host $Err) }
}

if ((Get-ManagementGroupConnection | Where-Object {$_.ManagementServerName -eq $RMS}) -eq $null)
{
New-ManagementGroupConnection $RMS -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-Host $Err) }
}

if ((Get-PSDrive | Where-Object {$_.Name -eq 'Monitoring'}) -eq $null)
{
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-Host $Err) }
}

Set-Location Monitoring:\$RMS
$Alerts = Get-Alert | where {$_.ID -eq $AlertID}
if ($Alerts.ProblemID -eq $null)
{
if ($Alerts.Count -gt 0)
{
$ProblemID = $Alerts[0].ProblemId.ToString()
$AlertName = $Alerts[0].Name
}
else
{
throw write-host "No alert was found with the specified name"
}
}
else
{
$ProblemId = $Alerts.ProblemId.ToString()
$AlertName = $Alerts.Name
}

## load SDK assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.Configuration")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.ConnectorFramework")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.Monitoring")

## connect to management group
$ManagementGroup = New-Object Microsoft.EnterpriseManagement.ManagementGroup($RMS)
$ManagementGroup.Reconnect()
$smtpAction = $ManagementGroup.GetNotificationAction("DefaultSmtpAction")
$recipient = $ManagementGroup.GetNotificationRecipient($NotificationRecipientName)
$config = New-Object Microsoft.EnterpriseManagement.Administration.AlertNotChangedSubscriptionConfiguration([Microsoft.EnterpriseManagement.Administration.AlertSubscriptionConfigurationType]::Any)
$config.Criteria = "<SimpleExpression><ValueExpression><Property>ProblemId</Property></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>$ProblemId</Value></ValueExpression></SimpleExpression>"
$config.ExpirationStartTime = Get-Date
$config.PollingIntervalMinutes = 1
$NewGuid = [System.Guid]::NewGuid().ToString().Replace('-', '_')
$Subscription = New-Object Microsoft.EnterpriseManagement.Administration.AlertNotificationSubscription("BriansCustomSubscription$NewGuid", $config)
$Subscription.DisplayName = $NotificationRecipientName + " - " + $AlertName
$Subscription.ToRecipients.Add($recipient)
$Subscription.Actions.Add($smtpAction)
$ManagementGroup.InsertNotificationSubscription($Subscription)

Create.A.Subscription.By.RightClicking.an.Alert.xml

<?xml version="1.0" encoding="utf-8"?><ManagementPack ContentReadable="true" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<Manifest>
<Identity>
<ID>Create.A.Subscription.By.RightClicking.an.Alert</ID>
<Version>1.0.0.0</Version>
</Identity>
<Name>Create A Subscription By Right-Clicking an Alert</Name>
<References>
<Reference Alias="SystemLibrary6062460">
<ID>System.Library</ID>
<Version>6.0.6246.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
<Reference Alias="SystemCenter">
<ID>Microsoft.SystemCenter.Library</ID>
<Version>6.0.6246.0</Version>
<PublicKeyToken>31bf3856ad364e35</PublicKeyToken>
</Reference>
</References>
</Manifest>
<Presentation>
<ConsoleTasks>
<ConsoleTask ID="ConsoleTaskGeneratedByUIcb448a12744345c19390a5fc2872683f" Accessibility="Public" Enabled="true" Target="SystemLibrary6062460!System.Entity" RequireOutput="true" Category="Alert">
<Application>%SystemRoot%\system32\windowspowershell\v1.0\powershell.exe</Application>
<Parameters>
<Parameter>c:\scripts\CreateSub.ps1</Parameter>
<Parameter>$Id$</Parameter>
</Parameters>
<WorkingDirectory>C:\Scripts</WorkingDirectory>
</ConsoleTask>
</ConsoleTasks>
<Folders>
<Folder ID="Folder_943fb409359542798564435b70d8a6aa" Accessibility="Public" ParentFolder="SystemCenter!Microsoft.SystemCenter.Monitoring.ViewFolder.Root" />
</Folders>
</Presentation>
<LanguagePacks>
<LanguagePack ID="ENG" IsDefault="false">
<DisplayStrings>
<DisplayString ElementID="Create.A.Subscription.By.RightClicking.an.Alert">
<Name>Create A Subscription By Right-Clicking an Alert</Name>
</DisplayString>
<DisplayString ElementID="Folder_943fb409359542798564435b70d8a6aa">
<Name>Create A Subscription By Right-Clicking an Alert</Name>
</DisplayString>
<DisplayString ElementID="ConsoleTaskGeneratedByUIcb448a12744345c19390a5fc2872683f">
<Name>Create a Subscription For this Problem</Name>
<Description>This is a task that can create a subscription for this problem</Description>
</DisplayString>
</DisplayStrings>
</LanguagePack>
<LanguagePack ID="ENU" IsDefault="false">
<DisplayStrings>
<DisplayString ElementID="Create.A.Subscription.By.RightClicking.an.Alert">
<Name>Create A Subscription By Right-Clicking an Alert</Name>
</DisplayString>
<DisplayString ElementID="Folder_943fb409359542798564435b70d8a6aa">
<Name>Create A Subscription By Right-Clicking an Alert</Name>
</DisplayString>
<DisplayString ElementID="ConsoleTaskGeneratedByUIcb448a12744345c19390a5fc2872683f">
<Name>Create a Subscription</Name>
<Description>Attempt to create an alert by right clicking a subscription
</Description>
</DisplayString>
</DisplayStrings>
</LanguagePack>
</LanguagePacks>
</ManagementPack>

Enjoy!

Brian McDermott
Escalation Engineer