# 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
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
  $Time = ((Get-Date).AddMonths(1))
  Start-SCOMMaintenanceMode -Instance $Group -EndTime $Time -Comment "Gateway Outage Maintenance Mode based on Group" -Reason "PlannedOther"
 
#Check to verify the Group is in maintenance mode
  [string]$MMComments = (Get-SCOMMaintenanceMode -Instance $Group).Comments
  IF ($MMComments -eq "Gateway Outage Maintenance Mode based on Group")
  {
   #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
  }
ELSE
{
  #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>