OpsMgr 2012 Self Maintenance Close Aged Rule Generated Alerts Probe

OpsMgr.2012.Self.Maintenance.Close.Aged.Rule.Generated.Alerts.Probe (ProbeActionModuleType)

Probe action module for closing aged rule generated alerts

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
PSScript ProbeAction Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelector
DaysToKeepint$Config/DaysToKeep$
CloseInfoAlertsbool$Config/CloseInfoAlerts$
CloseWarningAlertsbool$Config/CloseWarningAlerts$
CloseCriticalAlertsbool$Config/CloseCriticalAlerts$
UseLastModifiedDatebool$Config/UseLastModifiedDate$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<ProbeActionModuleType ID="OpsMgr.2012.Self.Maintenance.Close.Aged.Rule.Generated.Alerts.Probe" Accessibility="Internal" Batching="false" PassThrough="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="DaysToKeep" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="CloseInfoAlerts" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="CloseWarningAlerts" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="CloseCriticalAlerts" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="UseLastModifiedDate" type="xsd:boolean"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="DaysToKeep" Selector="$Config/DaysToKeep$" ParameterType="int"/>
<OverrideableParameter ID="CloseInfoAlerts" Selector="$Config/CloseInfoAlerts$" ParameterType="bool"/>
<OverrideableParameter ID="CloseWarningAlerts" Selector="$Config/CloseWarningAlerts$" ParameterType="bool"/>
<OverrideableParameter ID="CloseCriticalAlerts" Selector="$Config/CloseCriticalAlerts$" ParameterType="bool"/>
<OverrideableParameter ID="UseLastModifiedDate" Selector="$Config/UseLastModifiedDate$" ParameterType="bool"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="PSScript" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe">
<ScriptName>Close-AgedRuleAlerts.ps1</ScriptName>
<ScriptBody><Script>#===========================================================================================
# AUTHOR: Tao Yang
# Script Name: Close-AgedRuleAlerts.ps1
# DATE: 29/10/2012
# Version: 1.0
# COMMENT: - Script to close aged rule generated alerts
#===========================================================================================
Param ([int]$DaysToKeep,[string]$CloseInfoAlerts,[string]$CloseWarningAlerts,[string]$CloseCriticalAlerts,[string]$UseLastModifiedDate)

#Region FunctionLibs
function Load-SDK()
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager.Common") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.EnterpriseManagement.OperationsManager") | Out-Null
}
#EndRegion

#MOMScript API
$oAPI = New-Object -ComObject "MOM.ScriptAPI"
#Property Bag for result
$oBag = $oAPI.CreatePropertyBag()

#Connect to SCOM management group
Load-SDK
$MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings($Env:COMPUTERNAME)
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)

#work out alert criteria
#severity
If ($CloseInfoAlerts -ieq "false" -and $CloseWarningAlerts -ieq "false" -and $CloseCriticalAlerts -ieq "false")
{
#Incorrect configuration detected. no point to keep running if all 3 severities are excluded
$oAPI.LogScriptEvent("Close-AgedRuleAlerts.ps1", 9900, 2, "Incorrect configuration detected for the OpsMgr Self Maintenance Close Aged Rule Generated Alerts Rule. All 3 alert severities are configured to be excluded!" )
} else {
$strSeverityQuery = $null
If ($CloseInfoAlerts -ieq "false")
{
$strSeverityQuery = "Severity &lt;&gt; 0"
}
If ($CloseWarningAlerts -ieq "false")
{
if ($strSeverityQuery)
{
$strSeverityQuery = "$strSeverityQuery AND Severity &lt;&gt; 1"
} else {

$strSeverityQuery = "Severity &lt;&gt; 1"
}
}

If ($CloseCriticalAlerts -ieq "false")
{
if ($strSeverityQuery)
{
$strSeverityQuery = "$strSeverityQuery AND Severity &lt;&gt; 2"
} else {

$strSeverityQuery = "Severity &lt;&gt; 2"
}
}

#by default, use TimeRaised date
$EarliestDate = (Get-Date).AddDays(-$DaysToKeep)

If ($UseLastModifiedDate -ieq "true")
{
#use LastModified date instead of TimeRaised date if specified
$strCriteria = "ResolutionState &lt;&gt;`'255`' AND IsMonitorAlert = 'False' AND LastModified &lt;= `'$EarliestDate`'"
} else {
$strCriteria = "ResolutionState &lt;&gt;`'255`' AND IsMonitorAlert = 'False' AND TimeRaised &lt;= `'$EarliestDate`'"
}

If ($strSeverityQuery)
{
$strCriteria = "$strCriteria AND $strSeverityQuery"
}

$AlertCriteria = New-Object Microsoft.EnterpriseManagement.Monitoring.MonitoringAlertCriteria($strCriteria)
$Alerts = $MG.GetMonitoringAlerts($AlertCriteria)
$BeforeCount = $Alerts.Count

Foreach ($Alert in $Alerts)
{
$Alert.ResolutionState = 255
$Alert.Update("Closed by OpsMgr 2012 Self Maintenance Management Pack")
}

#Check Again
$Alerts = $MG.GetMonitoringAlerts($AlertCriteria)
$AfterCount = $Alerts.Count
If ($AfterCount -eq 0)
{
$Result = "Successful"
} elseif ($BeforeCount -gt 0 -and $AfterCount -gt 0 -and $AfterCount -le $BeforeCount) {
$Result = "PartialySuccessful"
} elseif ($BeforeCount -gt 0 -and $BeforeCount -eq $AfterCount) {
$Result = "Failed"
}
$oBag.AddValue('BeforeCount', $BeforeCount)
$oBag.AddValue('AfterCount', $AfterCount)
$oBag.AddValue('Result', $Result)
}
#$oAPI.Return($oBag)
$oBag</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>DaysToKeep</Name>
<Value>$Config/DaysToKeep$</Value>
</Parameter>
<Parameter>
<Name>CloseInfoAlerts</Name>
<Value>$Config/CloseInfoAlerts$</Value>
</Parameter>
<Parameter>
<Name>CloseWarningAlerts</Name>
<Value>$Config/CloseWarningAlerts$</Value>
</Parameter>
<Parameter>
<Name>CloseCriticalAlerts</Name>
<Value>$Config/CloseCriticalAlerts$</Value>
</Parameter>
<Parameter>
<Name>UseLastModifiedDate</Name>
<Value>$Config/UseLastModifiedDate$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PSScript"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>