SCOM Gateway Maintenance Mode Ping Monitor Recovery WriteAction

SCOM.GatewayMaintenanceMode.Ping.Monitor.Recovery.WA (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.BaseData

Member Modules:

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

Overrideable Parameters:

IDParameterTypeSelector
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="SCOM.GatewayMaintenanceMode.Ping.Monitor.Recovery.WA" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ComputerName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="Action" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="PSWA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>SCOM.GatewayMaintenanceMode.Ping.Monitor.Recovery.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Start or Stop Group Maintenance Mode Recovery Script
#
# Author: Kevin Holman
# Version: 1.2
#=================================================================================
param([string]$ComputerName,[string]$Action)


# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $ComputerName = "dmzomgw1.dmz.net"
# $Action = "Start"
#=================================================================================


# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging
$ScriptName = "SCOM.GatewayMaintenanceMode.Ping.Monitor.Recovery.WA.ps1"
$EventID = "1235"
#=================================================================================


# Starting Script section
#=================================================================================
# Gather the start time of the script
$StartTime = Get-Date
#Set variable to be used in logging events
$whoami = whoami
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
#=================================================================================


# Connect to local SCOM Management Group Section - If required
#=================================================================================
# I have found this to be the most reliable method to load SCOM modules for scripts running on Management Servers
# Clear any previous errors
$Error.Clear()
# Import the OperationsManager module and connect to the management group
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath
TRY
{
New-DefaultManagementGroupConnection -managementServerName "localhost"
}
CATCH
{
IF ($Error)
{
$momapi.LogScriptEvent($ScriptName,$EventID,1,"`n FATAL ERROR: Unable to load OperationsManager module or unable to connect to Management Server. `n Terminating script. `n Error is: ($Error).")
EXIT
}
}
#=================================================================================


# Begin MAIN script section
#=================================================================================
#Log script event that we are starting task
$Message = "`nRecovery script is starting for Computer: ($ComputerName). `nAction: ($Action) `nRunning as ($whoami)."
$momapi.LogScriptEvent($ScriptName,$EventID,0,$Message)
#Using Write-Host in a recovery action will output your data to the monitor statechange context in Health Explorer
Write-Host $Message

#Get Computer NetBIOSName
$ComputerNameSplit = $ComputerName.Split(".")
$ComputerNetBIOSName = $ComputerNameSplit[0]

#Get SCOM Group
$GroupName = "Gateway $ComputerNetBIOSName Assigned Computers Group"
$Group = Get-SCOMGroup -DisplayName $GroupName

IF ($Action -eq "Start")
{
#Put the SCOM Group into MM
#Check to see if GROUP is already in MM
$ExistingMM = Get-SCOMMaintenanceMode -Instance $Group
IF ($ExistingMM)
{
#Group object is already in MM.
$ExistingMMComments = $ExistingMM.Comments
$Message = "`nThe Group for Gateway: ($ComputerNetBIOSName) was already in Maintenance Mode."
$momapi.LogScriptEvent($ScriptName,$EventID,0,$Message)
Write-Host $Message
}
ELSE
{
# Place Group into MM - Set an end date 1 months out - Use special comment for identification
&#xA0; $Time = ((Get-Date).AddMonths(1))
&#xA0; Start-SCOMMaintenanceMode -Instance $Group -EndTime $Time -Comment "Gateway Outage Maintenance Mode based on Group" -Reason "PlannedOther"
&#xA0;
#Check to verify the Group is in maintenance mode
&#xA0; [string]$MMComments = (Get-SCOMMaintenanceMode -Instance $Group).Comments
&#xA0; IF ($MMComments -eq "Gateway Outage Maintenance Mode based on Group")
&#xA0; {
&#xA0;&#xA0; #Server is verified in MM
$Message = "`nThe Group named ($GroupName) was verified that it is in Gateway Outage Maintenance Mode."
$momapi.LogScriptEvent($ScriptName,$EventID,0,$Message)
Write-Host $Message
&#xA0; }
ELSE
{
&#xA0; #Something went wrong throw exception
$Message = "`nFATAL ERROR: We attempted to set Maintenance Mode for computer: ($GroupName) but were not able to confirm MM was set correctly."
$momapi.LogScriptEvent($ScriptName,$EventID,2,$Message)
Write-Host $Message
}
}
}

IF ($Action -eq "Stop")
{
#Remove the SCOM Group from MM
#Check to see if GROUP is in Special Gateway Group MM
$ExistingMM = Get-SCOMMaintenanceMode -Instance $Group
IF ($ExistingMM)
{
#Group object is in MM.
$ExistingMMComments = $ExistingMM.Comments
$Message = "`nThe Group for Gateway: ($ComputerNetBIOSName) was already in Maintenance Mode."
$momapi.LogScriptEvent($ScriptName,$EventID,0,$Message)
Write-Host $Message

IF ($ExistingMMComments -eq "Gateway Outage Maintenance Mode based on Group")
{
#Stop Maintenance Mode
Write-Host "Removing Group from MM"
$Group.StopMaintenanceMode([System.DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

#Add a check here to verify MM was removed.
}
ELSE
{
#Log that the group was already in MM for some other reason and no action will be taken
}
}
}
#=================================================================================


# End of script section
#=================================================================================
#Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript Completed. `nScript Runtime: ($ScriptTime) seconds.")
Write-Host "Script Completed. Script Runtime: ($ScriptTime) seconds."
#=================================================================================
# End of script
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>ComputerName</Name>
<Value>$Config/ComputerName$</Value>
</Parameter>
<Parameter>
<Name>Action</Name>
<Value>$Config/Action$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PSWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>