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

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

  • Comments 8
  • Likes

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="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://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

Comments
  • PingBack from http://geeklectures.info/2008/01/14/opsmgr-2007-creating-a-subscription-for-future-occurrences-of-an-alert/

  • http://blogs.technet.com/smsandmom/archive/2008/01/14/opsmgr-2007-creating-a-subscription-for-future

  • This was all great until I realized:

    I will have other alerts that need to go to other people/recipients/email adddresses.

    I wouldn't care if I was limited to a 1 alert to 1 recipient, but at this rate I am limited to:

    Any number of alerts to 1 recipient.

    Any thoughts on how to extend this? It seems like you were on the right track...

  • The SCOM 2007 BizTalk Management Pack has finally been released. The Management Pack is a complete rewrite

  • I was just at MMS-2008 and Dhananjay Mahajan presented the session "Optimizing Information Flow: Exploiting Notifications and Subscriptions

    ". He showed what I thought was this script but it did not include anything about importing a management pack. It also was shown that you run the script and specify the email address by passing that info with the script (not editing inside .ps1). This allows you to create alerts for more than one recipient. I am looking for that script and the command shell params to pass if anyone has it.

  • Followup:

    I did obtain the link from Microsoft for the other script. Basically there are 2 options at this point. The script and mgmt pack on this site will allow you to generate alerts for the 1 recipient via the GUI. The script on this page will allow you to pass the recipients via the command shell. I have not done this yet but understand that when you create these subscritions you cannot edit them via the console or they will break.

    http://code4ward.net/cs2/blogs/code4ward/archive/2007/09/19/set-notificationforalert.aspx

  • I used the script and management pack to create subscription but it capturing all alerts instead of the specific alert. Do I have to manually change the subscription? Here is my scenario:

    I have a file system with multiple volumes and I want to notify different groups based on the volume not the actual server. Can this be accomplished? Thanks.

  • thank you

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment