Microsoft SystemCenter Microsoft Azure Close Metric Alert

Microsoft.SystemCenter.MicrosoftAzure.CloseMetricAlert (WriteActionModuleType)

Microsoft SystemCenter Microsoft Azure Close Metric Alert.

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsSystem.PrivilegedMonitoringAccount
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
CDResolved ConditionDetection System.ExpressionFilter Default
WA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="Microsoft.SystemCenter.MicrosoftAzure.CloseMetricAlert" Accessibility="Public" Batching="true" RunAs="System!System.PrivilegedMonitoringAccount">
<Configuration>
<!--<xsd:element name="RuleId" type="xsd:string" />-->
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="0" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<ConditionDetection ID="CDResolved" TypeID="System!System.ExpressionFilter">
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">Property[@Name='Status']</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String">Resolved</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</ConditionDetection>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>CloseAlert.ps1</ScriptName>
<ScriptBody><Script>#RuleId - Alert for RuleId to be closed
param($RuleId)

$errorActionPreference = 'Stop'

$momAPI = New-Object -ComObject "MOM.ScriptAPI"

$errEvent = [System.Diagnostics.EventLogEntryType]::Error;
$infoEvent = [System.Diagnostics.EventLogEntryType]::Information;

#Alert Resolution State
$resolutionStateClose = 255;

$script:traceMsg = "";

#Adds tracemessage to $Script
Function AddTraceMessage
{
param($message)
$timeStamp = (get-date -format "HH:mm:ss:fff");
$script:traceMsg = $script:traceMsg + "`n[" + $timeStamp + "] " + $message;
}


Try
{
$error.Clear();

AddTraceMessage -message "Microsoft Azuew Metric Alert Rule CloseAlert.ps1 started."

if([string]::IsNullOrEmpty($RuleId))
{
Throw [System.NullReferenceException] "Null or empty FaultId parameter to the script.";
}

#Import-Module OperationsManager
$omModule = get-module -name "OperationsManager"
if ($omModule -eq $null)
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath | Out-Null
}

AddTraceMessage -message "Searching OM Alert with these patterns: `n $RuleId"


$Criteria = "MonitoringObjectName='" + $RuleId + "'"

$alert = Get-SCOMAlert -Criteria \"$Criteria\"

if($alert -ne $null)
{
AddTraceMessage -message ("Alert found. Name = " + $alert[0].Name)
Set-SCOMAlert -Alert $alert -ResolutionState $resolutionStateClose
AddTraceMessage -message "Alert is closed."
}
else
{
AddTraceMessage -message "Could not find OM Alert with given pattern."
}

AddTraceMessage -message "Microsoft Azuew Metric Alert Rule CloseAlert.ps1 completed."

}
Catch
{
AddTraceMessage -message $_.Exception.Message
}
Finally
{
if ($error -ne $null -and $error.Count -gt 0 -and $error[0] -ne $null)
{
$momAPI.LogScriptEvent("CloseAlert.ps1", 6420, 1, $error[0].ToString());
}

if ($script:traceMsg -ne $null)
{
$momAPI.LogScriptEvent("CloseAlert.ps1", 6421, 0, $script:traceMsg);
}
}</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>RuleId</Name>
<Value>$Data/Property[@Name='ResourceUri']$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA">
<Node ID="CDResolved"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>