<WriteActionModuleType ID="SCOMAgentHelper.TriggerWriteStatusEvent.WA" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AgentDisplayName" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SpreadInitializationOverIntervalSeconds" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WorkflowName" minOccurs="1" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WriteActionTimeoutSeconds" minOccurs="1" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="WriteToEventLog" minOccurs="1" type="xsd:boolean"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="WriteActionTimeoutSeconds" Selector="$Config/WriteActionTimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SpreadInitializationOverIntervalSeconds" Selector="$Config/SpreadInitializationOverIntervalSeconds$" ParameterType="int"/>
<OverrideableParameter ID="WriteToEventLog" Selector="$Config/WriteToEventLog$" ParameterType="bool"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="POSH_WriteStatusEvent" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>TriggerAgentTask-WriteMMStatus.ps1</ScriptName>
<ScriptBody><Script><#
Script: TriggerAgentTask-WriteMMStatus.ps1
Description: This is for the WriteAction on the mgmt server to trigger the agent task which will write the MM status to the event log
Author: Tyson Paul
Version History:
2020.08.04.1531 - Added 'Verify' param to enable immediate exit if $Verify -match 'Workflows|None'. No point running this task in this case.
Added logic so task would trigger if an existing MM got updated and the Verify Workflows param was used. The posh module on agent will depend on the event log as no workflow activity events will occur from an 'update' to a MM window.
2020.07.31.1401 - Added logic to account for no Action param value; will immediately trigger status message.
2020.07.22.1935 - Added SpreadInitializationOverIntervalSeconds, WaitForStatusToUpdateSeconds
2020.07.20 - Original
Note: Why use 'Verify=MgmtPerspectiveOnly'? This would be useful if user is rebooting/poweroff a Computer. There's no need to wait for workflows to unload. The only concern is that the
mgmt server knows the object is in MM, and thus it won't alert about heartbeats. This relatively fast response to the agent log will indicate that it's 'safe' to power off the Computer
without waiting for workflows to unload.
#>
Param (
[string]$Action,
[string]$AgentDisplayName,
# True|False. This indicates if the object was already in MM but was simply updated.
[string]$PreviousMMSettingsUpdated = 'False',
[Parameter(Mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false,
ValueFromRemainingArguments=$false,
ParameterSetName='Parameter Set 1')]
[ValidateSet("Workflows","MgmtPerspectiveOnly","None")]
[string]$Verify='MgmtPerspectiveOnly',
Function Get-CurrentLineNumber {
$MyInvocation.ScriptLineNumber
}
########################################################################################################
New-Alias -Name _LINE_ -Value Get-CurrentLineNumber -Description 'Returns the current line number in a PowerShell script file.' -ErrorAction SilentlyContinue
########################################################################################################
[int]$info = 0
[int]$Critical = 1
[int]$warn = 2
[string]$whoami = whoami.exe
$ScriptName ='TriggerAgentTask-WriteMMStatus.ps1'
$ThisScriptInstanceGUID = (New-Guid).Guid.Substring(((New-Guid).Guid.Length ) -6).ToUpper()
[int]$maxLogLength = 31000 #max chars to allow for event log messages
$pauseForStatusCheckSeconds = 15
<#
If the user indicated to Verify Workflows in combination with Force, this gets tricky. If the object was already in MM,
the duration would simply be updated but no workflow activity events would result on the agent so the powershell command would never verify/detect 1215/1216 events.
Check if the object was already in MM and duration was updated. If so, ignore 'Verify' settings, proceed with triggering task to write to event log.
The posh module on the agent will depend on this data because no workflow activity events will be written after a MM settings update.
#>
If ( ($Verify -match 'None') -OR ( ($Verify -match 'Workflows') -AND ($PreviousMMSettingsUpdated -match 'False') )) {
LogIt -EventID 9991 -Type $info -Message "Verify: [$($Verify)]. No point writing status to agent log. Abandoning this script. Exiting." -Proceed $WriteToEventLog -Line $(_LINE_)
Exit
}
# If Action is provided it is assumed that MM change was recently triggered, the script will try to wait for the status to be updated so that the resulting status message will be completely accurate.
# Otherwise it is assumed that the intention is to simply trigger a status message with no other action taken.
If ($Action -match 'Enable|Disable') {
switch ($Action){
'Enable' {
$ExpectedStatus = $true
}
'Disable' {
$ExpectedStatus = $false
}
}
If (-NOT (($WinComp.InMaintenanceMode) -eq $ExpectedStatus)) {
LogIt -EventID 9992 -Type $info -Message "WinComp.DisplayName: $($WinComp.DisplayName), 'InMaintenanceMode' status [$($WinComp.InMaintenanceMode.ToString())] is not yet changed to expected status [$($ExpectedStatus)]. Will keep checking every [$($pauseForStatusCheckSeconds)] seconds until timeout [$($WaitForStatusToUpdateSeconds) seconds] is expired. " -Proceed $WriteToEventLog -Line $(_LINE_)
}
$timer = [System.Diagnostics.Stopwatch]::StartNew()
# Loop for a reasonable amount of time to get expected MM status, based on "Action" parameter.
While ( (-NOT (($WinComp.InMaintenanceMode) -eq $ExpectedStatus)) -AND ($timer.Elapsed.TotalSeconds -le ($WaitForStatusToUpdateSeconds + $RandSleep) ) ) {
Start-Sleep -Seconds $pauseForStatusCheckSeconds
$WinComp = $MG.GetMonitoringObjects($objCriteria)
}
$timer.Stop()
}
# Set up override for task: status in param[1]. Include data indicating if an existing MM window was updated (param[2]).
$hashOverrides = @{
Message = "$($mmStatus)^PreviousMMSettingsUpdated:$($PreviousMMSettingsUpdated)"
}