In MOM 2005 it was easy to notify someone on specific alerts. SCOM 2007 - with it's subscription based system - cannot handle these kind of notifications. But the limitation is the user interface. Jakub from the SCOM 2007 product team released some very useful code which helped to implement a notification based on the alert name. One problem of Jakubs code is, that it's a C# program which is not very handy and that he creates most of the stuff in code which can be created with the UI.
I translated the script to power shell, which should be much friendlier to use, deploy and let's you quickly tweak some settings. The code below works for me (at least working with email notifications), but I have to admit, I didn't test it that much. The script usage is pretty simple, but you have to do some stuff first:
- Setup email notification
- Create at least one recipient
- The alert has to be raised at least once
- Change the $RMS variable in the script to your root management server
Then, just execute it in Power Shell: ".C:\Set-NotificationForAlert.ps1 "This is a test alert" "This is a test notification recipient"
Parameter 1: The name of the alert
Parameter 2: The name of the recipient
## check for parameters
param(
[string]$AlertName = $(throw write-host "You did not specify the -AlertName parameter."),
[string]$NotificationRecipientName = $(throw write-host "You did not specify the -NotificationRecipientName parameter. Use the Get-NotificationRecipient command to get a list of all registered notification recipients")
)
$RMS = "RootManagementServerName"
write-Host
write-Host "Executing Set-NotificationForAlert.ps1 ..."
write-Host
## prepare OpsMgr shell
if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null) {
Write-Host
Write-Host "File OpsMgrV3.ps1 loaded."
Write-Host "Initializing shell for operations manager..."
Write-Host "Add Microsoft.EnterpriseManagement.OperationsManager.Client snap in."
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) {
Write-Host "Connect to Management Server: $RMS"
New-ManagementGroupConnection $RMS -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-Host $Err) }
}
if ((Get-PSDrive | Where-Object {$_.Name -eq 'Monitoring'}) -eq $null) {
Write-Host "Create Monitoring drive from Provider."
New-PSDrive -Name: Monitoring -PSProvider: OperationsManagerMonitoring -Root: \ -ErrorAction SilentlyContinue -ErrorVariable Err
if ($Err) { $(throw write-Host $Err) }
Write-Host "Operations manager shell initialized."
Write-Host
}
Set-Location Monitoring:\$RMS
$Alerts = Get-Alert | where {$_.Name -eq $AlertName}
if ($Alerts.ProblemId -eq $null)
{
if ($Alerts.Count -gt 0)
{
$ProblemID = $Alerts[0].ProblemId.ToString()
}
else
{
throw write-host "No alert was found with the specified name"
}
}
else
{
$ProblemId = $Alerts.ProblemId.ToString()
}
## 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.AlertChangedSubscriptionConfiguration([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()
$NewGuid = $NewGuid.ToString().Replace('-', '_')
$Subscription = New-Object Microsoft.EnterpriseManagement.Administration.AlertNotificationSubscription("STACustomSubscription$NewGuid", $config)
$Subscription.DisplayName = $NotificationRecipientName + " - " + $AlertName
$Subscription.ToRecipients.Add($recipient)
$Subscription.Actions.Add($smtpAction)
$ManagementGroup.InsertNotificationSubscription($Subscription)
So, hopefully I can ease some pain with this...