Microsoft.SystemCenter.Agent.MaintenanceMode.Trigger.Rule.WriteActionModuleType (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsSystem.PrivilegedMonitoringAccount
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
TimeoutSecondsint$Config/TimeoutSeconds$
Loggingstring$Config/Logging$
EventQueryIntervalInSecondsint$Config/EventQueryIntervalInSeconds$

Source Code:

<WriteActionModuleType ID="Microsoft.SystemCenter.Agent.MaintenanceMode.Trigger.Rule.WriteActionModuleType" Accessibility="Public" Batching="false" RunAs="System!System.PrivilegedMonitoringAccount">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Logging" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="EventQueryIntervalInSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="Logging" Selector="$Config/Logging$" ParameterType="string"/>
<OverrideableParameter ID="EventQueryIntervalInSeconds" Selector="$Config/EventQueryIntervalInSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>ReadAgentMaintenanceModeSystemEvents.ps1</ScriptName>
<ScriptBody><Script>
Param($Logging, $EventQueryIntervalInSeconds)

# Variables
################################################################
$EventLogPrefix = ""
$RuleName = "Microsoft.SystemCenter.Agent.MaintenanceMode"
$ScriptName = "ReadAgentMaintenanceModeSystemEvents.ps1"
$LoggingEventID = 2222
$LoggingEventInfomational = 0
$LoggingEventError = 1
################################################################

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

#Logging function
Function Log-Event ($Message, $LoggingEventSeverity)
{
$api.LogScriptEvent($EventLogPrefix+ " [" + $ScriptName + "]", $LoggingEventID, $LoggingEventSeverity, $Message)
}

$ErrorActionPreference = "SilentlyContinue"
try
{

#Import SCOM Module
$setupKey = Get-Item -Path "HKLM:\Software\Microsoft\Microsoft Operations Manager\3.0\Setup"
$installDirectory = $setupKey.GetValue("InstallDirectory") | Split-Path
$psmPath = $installdirectory + &#x2018;\Powershell\OperationsManager\OperationsManager.psm1&#x2019;
Import-Module $psmPath

$rule = Get-SCOMRule -Name $RuleName
$Events = Get-SCOMEvent -Rule $rule | Sort-Object -Descending TimeAdded | Where {$_.TimeAdded -gt (Get-Date).ToUniversalTime().AddSeconds(-$EventQueryIntervalInSeconds)} | Sort-Object TimeAdded

if($Logging -eq "true"){Log-Event "Number of Events to process: $($events.Count)" $LoggingEventInfomational }

$computerClass = $null;
$computerClass = Get-SCOMClass -Name "System.Computer"

Foreach ($Event in $Events)
{
$computerInstance = $null;
$LoggingComputer = $Event.LoggingComputer
$EventData = [xml]$Event.EventData
[double]$Duration = $EventData.DataItem.EventData.Data[0]
$Reason = $EventData.DataItem.EventData.Data[1]
$UserString = $EventData.DataItem.EventData.Data[2]
$Comment = $EventData.DataItem.EventData.Data[3]


if($Reason -eq "PlannedOther" -or $Reason -eq "UnplannedOther" -or $Reason -eq "PlannedHardwareMaintenance" -or $Reason -eq "UnplannedHardwareMaintenance" -or $Reason -eq "PlannedHardwareInstallation" -or $Reason -eq "UnplannedHardwareInstallation" -or $Reason -eq "PlannedOperatingSystemReconfiguration" -or $Reason -eq "UnplannedOperatingSystemReconfiguration" -or $Reason -eq "PlannedApplicationMaintenance" -or $Reason -eq "UnplannedApplicationMaintenance" -or $Reason -eq "ApplicationInstallation" -or $Reason -eq "ApplicationUnresponsive" -or $Reason -eq "ApplicationUnstable" -or $Reason -eq "SecurityIssue" -or $Reason -eq "LossOfNetworkConnectivity")
{
Log-Event "Processing: EventID: $($Event.Number) `nSource: $($Event.PublisherName) `nLoggingComputer: $LoggingComputer `nEventData: [$($Event.EventData)]" $LoggingEventInfomational
$computerInstance = Get-SCOMClassInstance -Class $computerClass | ? {$_.Name -eq $LoggingComputer}

if($computerInstance -ne $null)
{
[datetime]$CurTime=Get-Date;
if(5.0001 -gt $Duration -and 5.0 -le $Duration)
{
$Duration = 5.0001;
}
[datetime]$EndDatetime = ($CurTime.AddMinutes($Duration));


if ($computerInstance.InMaintenanceMode -eq $true)
{
{Log-Event "computerInstance.InMaintenanceMode true" $LoggingEventInfomational }
[datetime]$EndDatetimeUTC = $EndDatetime.ToUniversalTime()
$computerInstance.UpdateMaintenanceMode($EndDatetimeUTC, [Microsoft.EnterpriseManagement.Monitoring.MaintenanceModeReason]::$Reason, $Comment, [Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive)
#Log-Event "Maintenance Mode updated for $LoggingComputer to $EndDatetime" $LoggingEventInfomational
$api.LogScriptEvent($EventLogPrefix+ " [" + $ScriptName + "]", 2223, $LoggingEventInfomational, "Maintenance Mode updated for $LoggingComputer to $EndDatetime")
}
else
{
Start-SCOMMaintenanceMode -Instance $computerInstance -EndTime $EndDatetime -Reason $Reason -Comment $Comment -ErrorAction SilentlyContinue;
#Log-Event "Maintenance Mode set for $LoggingComputer to $EndDatetime" $LoggingEventInfomational
$api.LogScriptEvent($EventLogPrefix+ " [" + $ScriptName + "]", 2223, $LoggingEventInfomational, "Maintenance Mode set for $LoggingComputer to $EndDatetime $Reason $Comment")
}
} #if($computerInstance -ne $null)
} #if($Reason
} #for loop
} # try
Catch
{
$ErrorMessage = $_.Exception.Message
Log-Event $ErrorMessage $LoggingEventError
}
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>Logging</Name>
<Value>$Config/Logging$</Value>
</Parameter>
<Parameter>
<Name>EventQueryIntervalInSeconds</Name>
<Value>$Config/EventQueryIntervalInSeconds$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>