SCOM.Addons.MailIn.CloseAlerts.Rule.Datasource (DataSourceModuleType)

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
Scheduler DataSource System.Scheduler Default
PA ProbeAction Microsoft.Windows.PowerShellPropertyBagProbe Default

Overrideable Parameters:

IDParameterTypeSelector
IntervalSecondsint$Config/IntervalSeconds$
SyncTimestring$Config/SyncTime$
TimeoutSecondsint$Config/TimeoutSeconds$
AlertRetentionHoursint$Config/AlertRetentionHours$

Source Code:

<DataSourceModuleType ID="SCOM.Addons.MailIn.CloseAlerts.Rule.Datasource" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" type="xsd:integer" name="IntervalSeconds"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" type="xsd:string" name="SyncTime"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" type="xsd:integer" name="TimeoutSeconds"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" type="xsd:integer" name="AlertRetentionHours"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SyncTime" Selector="$Config/SyncTime$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="AlertRetentionHours" Selector="$Config/AlertRetentionHours$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="Scheduler" TypeID="System!System.Scheduler">
<Scheduler>
<SimpleReccuringSchedule>
<Interval Unit="Seconds">$Config/IntervalSeconds$</Interval>
<SyncTime>$Config/SyncTime$</SyncTime>
</SimpleReccuringSchedule>
<ExcludeDates/>
</Scheduler>
</DataSource>
<ProbeAction ID="PA" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>Clean-GenericMailAlerts.ps1</ScriptName>
<ScriptBody><Script>param($sourceId, $managedEntityId, $AlertRetentionHours)

$api = New-Object -ComObject 'MOM.ScriptAPI'

$Global:Error.Clear()
$ErrorActionPreference = 'Continue'

$api.LogScriptEvent('SCOM.Addons.MailIn Clean-GenericMailAlerts.ps1',6000,4,"SCOM Addons MailIn Clean-GenericMailAlerts Rule Started - Source $($sourceId) `
managEnt $($managedEntityId) #RetentionHours:$($AlertRetentionHours) ")


$allGenericMailAlerts = 0
$closedGenericMailAlerts = 0
$alertRuleId = Get-SCOMRule -Name 'SCOM.Addons.MailIn.Generic.Alert.Rule' | Select-Object -ExpandProperty Id
$allOpenAlerts = Get-ScomAlert -ResolutionState 0 | Where-Object {$_.RuleId -eq $alertRuleId}

$allGenericMailAlerts = $allOpenAlerts.count

if ($allGenericMailAlerts -gt 1) {

foreach ($alert in $allOpenAlerts) {

$timeSinceAlertInHours = (New-Timespan -Start $alert.TimeRaised -End (Get-Date)).TotalHours -as [int]

If ($timeSinceAlertInHours -ge $AlertRetentionHours ) {
$alertComment = "Autoclosed by rule SCOM.Addons.MailIn.CloseAlerts.Rule. AlertRentionInHours: $($AlertRetentionHours) - `
Time since raise in hours: $($timeSinceAlertInHours) "
$alert | Set-SCOMAlert -ResolutionState 255 -Comment $alertComment
$closedGenericMailAlerts = $closedGenericMailAlerts + 1
}

}
}

$genericAlertInfo = "All Generic Mail Alerts: $($allGenericMailAlerts), Closed: $($closedGenericMailAlerts)"



$allCleanEmlArchiveAlerts = 0
$closedCleanEmlArchiveAlerts = 0
$alertRuleId = Get-SCOMRule -Name 'SCOM.Addons.MailIn.CleanEmlArchive.Rule' | Select-Object -ExpandProperty Id
$allOpenAlerts = Get-ScomAlert -ResolutionState 0 | Where-Object {$_.RuleId -eq $alertRuleId}

$allCleanEmlArchiveAlerts = $allOpenAlerts.count
if ($allCleanEmlArchiveAlerts -gt 1) {
foreach ($alert in $allOpenAlerts) {

$timeSinceAlertInHours = (New-Timespan -Start $alert.TimeRaised -End (Get-Date)).TotalHours -as [int]

If ($timeSinceAlertInHours -gt 1) {
$alertComment = "Autoclosed by rule SCOM.Addons.MailIn.CloseAlerts.Rule. Info messges closed if older than 1 hour. - `
Time since raise in hours: $($timeSinceAlertInHours) "
$alert | Set-SCOMAlert -ResolutionState 255 -Comment $alertComment
$closedCleanEmlArchiveAlerts = $closedCleanEmlArchiveAlerts + 1
}

}
}

$cleanEmlArchiveAlertInfo = "All Clean Eml Archive Alerts: $($allCleanEmlArchiveAlerts), Closed: $($closedCleanEmlArchiveAlerts)"



$allCloseAlertsAlerts = 0
$closedCloseAlertsAlerts = 0
$alertRuleId = Get-SCOMRule -Name 'SCOM.Addons.MailIn.CloseAlerts.Rule' | Select-Object -ExpandProperty Id
$allOpenAlerts = Get-ScomAlert -ResolutionState 0 | Where-Object {$_.RuleId -eq $alertRuleId}
$allCloseAlertsAlerts = $allOpenAlerts.count

if ($allCloseAlertsAlerts -gt 1) {
foreach ($alert in $allOpenAlerts) {

$timeSinceAlertInHours = (New-Timespan -Start $alert.TimeRaised -End (Get-Date)).TotalHours -as [int]

If ($timeSinceAlertInHours -gt 1) {
$alertComment = "Autoclosed by rule SCOM.Addons.MailIn.CloseAlerts.Rule. Info messges closed if older than 1 hour. - `
Time since raise in hours: $($timeSinceAlertInHours) "
$alert | Set-SCOMAlert -ResolutionState 255 -Comment $alertComment
$closedCloseAlertsAlerts = $closedCloseAlertsAlerts + 1
}

}
}

$closedAlertsAlertInfo = "All CloseAlerts Alerts: $($allCloseAlertsAlerts), Closed: $($closedCloseAlertsAlerts)"

$api.LogScriptEvent('SCOM.Addons.MailIn Clean-GenericMailAlerts.ps1',6001,4,"SCOM Addons MailIn Clean-GenericMailAlerts Rule `
`nGenericAlertInfo: $($genericAlertInfo) `
`nCleanEmlArchiveAlertInfo: $($cleanEmlArchiveAlertInfo) `
`nClosedAlertsAlertInfo: $($closedAlertsAlertInfo) ")

$title = 'This rule (SCOM.Addons.MailIn.CloseAlerts.Rule) closes all GenericAlertMails older than: ' + $AlertRetentionHours + ' hours. All info messages are deleted after 1 hour.'

if ($allGenericMailAlerts -gt 1) {
$bag = $api.CreatePropertybag()
$bag.AddValue("Title",$title)
$bag.AddValue("GenericAlertInfo",$genericAlertInfo)
$bag.AddValue("CleanEmlArchiveAlertInfo",$cleanEmlArchiveAlertInfo)
$bag.AddValue("ClosedAlertsAlertInfo",$closedAlertsAlertInfo)
$bag.AddValue("Result","BAD")
$bag
} else {
$foo = 'No bag. - Rule message with 0 does not make sense.'
}

</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>AlertRetentionHours</Name>
<Value>$Config/AlertRetentionHours$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PA">
<Node ID="Scheduler"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>