Tuesday, 24 May 2011

SCOM 2007 SP1 per rule notifications

None of this is necessary if using SCOM 2007 R2 as the GUI can perform the steps this script does, subscriptions created with this script can be opened with an R2 console and look as you would expect, however if the subscription is opened from an SP1 console the XML will break and the subscription will no longer work as expected.
After spending ages trying to work out how to create per rule notifications and then discovering it wasn't possible using the GUI I resorted to writing my own Powershell script complete with GUI (also written in powershell, thanks to PrimalForms for the GUI) for this very task. Feel free to use this as you see fit. To use the script you first need to edit change the $RMS to be your RMS server. There is some tweaking required if you want to respond to a monitor instead. I'll post the amendments at a later date.


#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms v1.0.2.0
# Generated On: 20/10/2009 09:55
# Generated By: ########################################################################

$RMS = "scomprodrms"

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button4 = New-Object System.Windows.Forms.Button
$textBox2 = New-Object System.Windows.Forms.TextBox
$listBox2 = New-Object System.Windows.Forms.ListBox
$label2 = New-Object System.Windows.Forms.Label
$textBox1 = New-Object System.Windows.Forms.TextBox
$listBox1 = New-Object System.Windows.Forms.ListBox
$label1 = New-Object System.Windows.Forms.Label
$button1 = New-Object System.Windows.Forms.Button
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{
#Populate listboxes
#TODO: Place custom script here
write-host Searching...

    $listbox1.items.clear()
    $listbox2.items.clear()

    if ((Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.EnterpriseManagement.OperationsManager.Client'}) -eq $null)
    {
        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
   
    $rules = Get-Rule | where {$_.displayname -match $textbox1.text}

    foreach ($r in $rules)
        {
        $listbox1.items.add($r.displayname)
        }
       
    $notifications = Get-NotificationRecipient | where {$_.name -match $textbox2.text}
   
    foreach ($n in $notifications)
        {
        $listbox2.items.add($n.name)
        }
       
}

$button4_OnClick=
{
#Create subscription
#TODO: Place custom script here
    ## 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()
   
    write-host "Getting rule id for"
    write-host $listbox1.selecteditem
    $MonitoringRuleId = (Get-Rule | where {$_.displayname -eq $listbox1.selecteditem}).id.tostring()
    write-host $MonitoringRuleId

    write-host   
   
    write-host "Getting recipient details for"
    write-host $listbox2.selecteditem
    $recipient = $ManagementGroup.GetNotificationRecipient($listbox2.selecteditem)
    write-host $recipient
   
   
    $config = New-Object Microsoft.EnterpriseManagement.Administration.AlertChangedSubscriptionConfiguration([Microsoft.EnterpriseManagement.Administration.AlertSubscriptionConfigurationType]::Any)

    #$config.Criteria = "<SimpleExpression><ValueExpression><Property>RuleId</Property></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>$MonitoringRuleId</Value></ValueExpression></SimpleExpression>"
    $config.Criteria = "<And><Expression><SimpleExpression><ValueExpression><Property>RuleId</Property></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>$MonitoringRuleId</Value></ValueExpression></SimpleExpression></Expression><Expression><SimpleExpression><ValueExpression><Property>ResolutionState</Property></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>0</Value></ValueExpression></SimpleExpression></Expression></And>"

    $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 = "****  Do not open in OpsMgr GUI - " + $listbox1.selecteditem + " - " + $listbox2.selecteditem
    $Subscription.ToRecipients.Add($recipient)

#Individual actions are no longer used as we now add all actions to the subscription
#    $smtpAction = $ManagementGroup.GetNotificationAction("DefaultSmtpAction")
#    $alarmpointAction = $ManagementGroup.GetNotificationAction("Cmd0d2dcdf3_455b_4641_8bd9_6b2c6f64bce7")

    $ManagementGroup.GetNotificationActions() | foreach {
            $Subscription.Actions.Add($_)
    }
   
    $ManagementGroup.InsertNotificationSubscription($Subscription)
    write-host
    write-host Subscription created as $Subscription.DisplayName
    write-host
    write-host To create a new subscription enter new criteria
    write-host
   
}

#----------------------------------------------
#region Generated Form Code
$form1.Text = 'SCOM Rule Notifications'
$form1.Name = 'form1'
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 573
$System_Drawing_Size.Height = 284
$form1.ClientSize = $System_Drawing_Size

$button4.TabIndex = 6
$button4.Name = 'button4'
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 144
$System_Drawing_Size.Height = 23
$button4.Size = $System_Drawing_Size
$button4.UseVisualStyleBackColor = $True

$button4.Text = 'Create Subscription'

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 204
$System_Drawing_Point.Y = 234
$button4.Location = $System_Drawing_Point
$button4.DataBindings.DefaultDataSourceUpdateMode = 0
$button4.add_Click($button4_OnClick)

$form1.Controls.Add($button4)

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 275
$System_Drawing_Size.Height = 20
$textBox2.Size = $System_Drawing_Size
$textBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox2.Name = 'textBox2'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 38
$textBox2.Location = $System_Drawing_Point
$textBox2.TabIndex = 1

$form1.Controls.Add($textBox2)

$listBox2.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 239
$System_Drawing_Size.Height = 134
$listBox2.Size = $System_Drawing_Size
$listBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox2.Name = 'listBox2'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 297
$System_Drawing_Point.Y = 85
$listBox2.Location = $System_Drawing_Point
$listBox2.TabIndex = 5

$form1.Controls.Add($listBox2)

$label2.TabIndex = 5
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 100
$System_Drawing_Size.Height = 23
$label2.Size = $System_Drawing_Size
$label2.Text = 'Recipients'

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 18
$System_Drawing_Point.Y = 41
$label2.Location = $System_Drawing_Point
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Name = 'label2'

$form1.Controls.Add($label2)

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 275
$System_Drawing_Size.Height = 20
$textBox1.Size = $System_Drawing_Size
$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox1.Name = 'textBox1'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 12
$textBox1.Location = $System_Drawing_Point
$textBox1.TabIndex = 0

$form1.Controls.Add($textBox1)

$listBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 239
$System_Drawing_Size.Height = 134
$listBox1.Size = $System_Drawing_Size
$listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox1.Name = 'listBox1'
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 85
$listBox1.Location = $System_Drawing_Point
$listBox1.TabIndex = 4

$form1.Controls.Add($listBox1)

$label1.TabIndex = 1
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 100
$System_Drawing_Size.Height = 23
$label1.Size = $System_Drawing_Size
$label1.Text = 'Rules'

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 18
$System_Drawing_Point.Y = 15
$label1.Location = $System_Drawing_Point
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$label1.Name = 'label1'

$form1.Controls.Add($label1)

$button1.TabIndex = 3
$button1.Name = 'button1'
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 125
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

$button1.Text = 'Get Data from SCOM'

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 368
$System_Drawing_Point.Y = 35
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

#endregion Generated Form Code

#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
write-host Watch both the powershell console and the GUI for informational messages
GenerateForm

No comments:

Post a Comment