SCRIPT: Schedule Maintenance Mode SCOM 2012 R2

I know scheduled maintenance mode functionality is coming to SCOM 2016 as shown here. However I have clients that are still on SCOM 2012 R2 that need to schedule maintenance mode in SCOM right now. There are options out there for this already the best solution is a tool called SCOM 2012 Maintenance Mode Scheduler . It has a small fee but is well worth it. Sometimes I have clients that would prefer to run a script vs. pay the fee for a tool especially when this is coming to SCOM 2016.

There are a bunch of scripts out there for this but most are old and or don’t work. I have been using my own script to do this for a while. I recently updated it and have decided to share it. I run this script from Windows task scheduler. It could also just as easily be scheduled to run from Orchestrator, SMA or Azure Automation. I will put the download link at the end of this blog post. This script will put a SCOM group in maintenance mode. It also sets the duration in minutes but can easily be changed to hours.

Here is the PowerShell script:

<#

#########================================================================================================#########

#

#

# ** Name: SCOMGroupMaintMode.ps1

# ** Author: System Center MVP – Steve Buchanan

# ** Date: 7/30/2015

# ** Version: 1.4

# ** Website: www.buchatech.com

#

# ** Description:

# ** This script can be used to a group of objects within SCOM into Maintenance Mode.

# ** This script needs to be run in an administrative PowerShell console.

# ** This script needs to be run with some mandatory parameters. Parameters shown in examples.

#

############################################

Example when running from PowerShell window:

############################################

-ExecutionPolicy Bypass .\SCOMGroupMaintMode.ps1 -OMServer SCOMSERVERNAME.DOMAINNAME.COM -GroupName ‘NAMEOFYOURGROUP’ -Duration ENTERMINUTESFORDURATION -Reason PlannedOther -Comment ‘ADDCOMMENTIFYOUWANT’

#################################################################################

Example of syntax when placing in Windows Task Scheduler (Actions>>Ad arguments):

#################################################################################

-ExecutionPolicy Bypass C:\LOCATIONOFSCRIPT\SCOMGroupMaintMode.ps1 -OMServer SCOMSERVERNAME.DOMAINNAME.COM -GroupName ‘All Windows Computers’ -Duration 10 -Reason PlannedOther -Comment ‘ADDCOMMENTIFYOUWANT’

###############

Parameters are:

###############

-OMServer

Mandatory parameter containing mgmt server name (Be sure to use FQDN).

-GroupName

Mandatory parameter containing display name of the target group (Be sure to enclose with ”).

-Duration

Mandatory parameter containing integer of desired duration in minutes.

-Reason

Mandatory parameter containing reason. The acceptable values for this parameter are:

— PlannedOther

— UnplannedOther

— PlannedHardwareMaintenance

— UnplannedHardwareMaintenance

— PlannedHardwareInstallation

— UnplannedHardwareInstallation

— PlannedOperatingSystemReconfiguration

— UnplannedOperatingSystemReconfiguration

— PlannedApplicationMaintenance

— ApplicationInstallation

— ApplicationUnresponsive

— ApplicationUnstable

— SecurityIssue

— LossOfNetworkConnectivity

-Comment

Optional parameter description of maintenance action (Free text. Be sure to enclose with ”).

#########================================================================================================#########

#>

###########################

## General Script Settings

###########################

# Error Handling

$ErrorActionPreference = “SilentlyContinue”

# Set Parameters

Param (

[Parameter(Mandatory=$true)][string]$OMServer,

[Parameter(Mandatory=$true)][string]$GroupName,

[Parameter(Mandatory=$true)][Int32]$Duration,

[Parameter(Mandatory=$true)][string]$Reason,

[Parameter(Mandatory=$false)][string]$Comment

)

####################################

# Load needed PowerShell modules

####################################

# Load Operations Manager module

Import-Module -Name OperationsManager

##################

## Start Script

##################

# Make Connection to SCOM Server

New-SCOMManagementGroupConnection -ComputerName $OMServer

$RetriveGroup = (Get-SCOMGroup | where {$_.DisplayName -like “*$GroupName*”})

# Use this for minutes. If you want hours instead comment this line out and use line below.

$Time = ((Get-Date).AddMinutes($Duration))

# Use this for hours. If you want minutes instead comment this line out and use line above.

#$Time = ((Get-Date).AddHours($Duration))

Start-SCOMMaintenanceMode -Instance $RetriveGroup -EndTime $Time -Comment “$Comment” -Reason “$Reason”

# Variable cleanup

Remove-Variable -Name *

##################

## End Script

##################

Create group in SCOM to use in script:

You need to have a group created in SCOM with the servers you want to be put into maintenance mode added as members. This is what will be targeted in the script. For this example our group is called MaintModeServers. Note that you could also use one of the default groups for example All Windows Computers or SQL Server 2012 Computers or even a single server.

clip_image001

Run as scheduled task:

The easiest most simply way to schedule this is through Windows Task Scheduler. Here are some screenshots for manually creating the scheduled task. I also have included an XML of a scheduled task that you could simply modify and import into your server.

Option #1 Manually create your scheduled task.

clip_image002

Build the schedule/s you want in Triggers.

clip_image003

You need to add the path to PowerShell and the syntax including parameters in the Actions tab. Here is what you need to add along with a screenshot.

PROGRAM SCRIPT:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

NOTE: You need to ensure you add the parameters highlighted in yellow just like you would if running this from PowerShell.

ADD ARGUMENTS:

-ExecutionPolicy Bypass C:\Tools\Scripts\SCOMGroupMaintMode.ps1 -OMServer OM01.buchatech.com -GroupName ‘MaintModeServers‘ -Duration 10 -Reason PlannedOther -Comment ‘This was set by a SCOM maintenance mode script.

clip_image004

Option #2 Import the scheduled task and modify settings.

The XML For scheduled task is below. Settings you should change are highlighted in yellow.

<?xml version=”1.0″ encoding=”UTF-16″?>

<Task version=”1.2″ xmlns=”http://schemas.microsoft.com/windows/2004/02/mit/task”>

<RegistrationInfo>

<Date>2015-07-31T01:31:42.1826916</Date>

<Author>BUCHATECH\administrator</Author>

</RegistrationInfo>

<Triggers>

<CalendarTrigger>

<StartBoundary>2015-07-31T09:30:13</StartBoundary>

<ExecutionTimeLimit>PT2H</ExecutionTimeLimit>

<Enabled>true</Enabled>

<ScheduleByMonthDayOfWeek>

<Weeks>

<Week>3</Week>

</Weeks>

<DaysOfWeek>

<Thursday />

</DaysOfWeek>

<Months>

<January />

<February />

<March />

<April />

<May />

<June />

<July />

<August />

<September />

<October />

<November />

<December />

</Months>

</ScheduleByMonthDayOfWeek>

</CalendarTrigger>

</Triggers>

<Principals>

<Principal id=”Author”>

<UserId>BUCHATECH\administrator</UserId>

<LogonType>Password</LogonType>

<RunLevel>HighestAvailable</RunLevel>

</Principal>

</Principals>

<Settings>

<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>

<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>

<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>

<AllowHardTerminate>true</AllowHardTerminate>

<StartWhenAvailable>false</StartWhenAvailable>

<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>

<IdleSettings>

<StopOnIdleEnd>true</StopOnIdleEnd>

<RestartOnIdle>false</RestartOnIdle>

</IdleSettings>

<AllowStartOnDemand>true</AllowStartOnDemand>

<Enabled>true</Enabled>

<Hidden>false</Hidden>

<RunOnlyIfIdle>false</RunOnlyIfIdle>

<WakeToRun>false</WakeToRun>

<ExecutionTimeLimit>P3D</ExecutionTimeLimit>

<Priority>7</Priority>

</Settings>

<Actions Context=”Author”>

<Exec>

<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>

<Arguments>-ExecutionPolicy Bypass C:\Tools\Scripts\SCOMGroupMaintMode.ps1 -OMServer OM01.buchatech.com -GroupName ‘MaintModeServers‘ -Duration 10 -Reason PlannedOther -Comment ‘This was set by a SCOM maintenance mode script.‘</Arguments>

</Exec>

</Actions>

</Task>

Import the scheduled task with PowerShell:

Register-ScheduledTask -Xml (get-content “C:\tools\Scripts\SCOM Scheduled Maintenance Mode.xml” | out-string) -TaskName “SCOM Scheduled Maintenance Mode” -User buchatech\administrator -Password PASSWORDGOESHERE –Force

Or import scheduled task through the GUI:

clip_image005

Download script and scheduled task XML here:

 https://gallery.technet.microsoft.com/Schedule-SCOM-Maintenance-7e60b8e1

Print Friendly, PDF & Email

Leave a Comment