Auto Maintenance Mode with SCOM 2012

<UPDATE> 2014-09-10 – I have updated a lot of things in this article. Please make sure you review it if you tried to implement it before. Main updates include:

– Proper command lines (the back-slashes were missing for some reason)

– Note about the Action account

– Note about excluding the management servers from the rule.

– Script has been updated with more error handling.

</UPDATE>

Very often I get the question about how to handle maintenance mode. Among the many options, it seems that having the server put in maintenance mode automatically solves many of the daily problems of the it admins.

There was a script flying around to do that, with some instructions scattered on how to deploy it. I implemented this at a customer this week and took the opportunity to update the script to the new 2012 cmd-lets.

Here’s how the process goes:

  • Copy the script to the c:scripts folder on all SCOM 2012 management servers.
  • Create an Alert rule
  • Create a channel
  • Create a subscriber
  • Create a subscription
  • Test

First, create the notification channel:

image

image

image

Here go the lines:

Full path of the command file:

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

Command line parameters:

C:\scriptsmaintenance_mode2.ps1 $Data/Context/DataItem/AlertId$ $Data/Context/DataItem/ManagedEntityDisplayName$

Startup folder for the command line:

c:\scripts

Now create the rule to pick-up event 1074, which is generated whenever the server is requested to be rebooted by a process:

image

In the authoring section of the SCOM Console:

image

imageimageimageimageimage

imageimage

Create the subscriber. Even though there is no actual user, you still need to do that in order to be triggered:

imageimageimageimageimage

Now create the subscription.

imageimageimage

IMPORTANT NOTES!

1. Before you test it, make sure you exclude the SCOM management servers, since you don’t want to put your management servers in maintenance mode. The effects can be very bad:

SCOMOverride

2. When the notification workflow runs, it will run as your default action action. Make sure that account is a SCOM administrator and that is has rights in the local path of the server where the script is located.

In order to test it, without actually rebooting a server:

image

You can download the script here.

Script:

#

# SCHEDULE MAINTENANCE MODE

# Source: http://lucamihailescu.blogspot.com/2010/01/operations-manager-maintenance-mode.html

# rewritten for new SCOM 2012 cmd-lets by Jose Fehse – 11/05/2013

# Updated 2014-09-10

Param($alertid, $computerPrincipalName, $minutes, $comment)

if (!$minutes) {$minutes = 15}

if (!$comment) {$comment = “Automatic Computer Restart Maintenance Mode”}

# Close Alert (otherwise no further notification will be fired)

function fixalert {

$alert = Get-ScomAlert -Id “$alertid”

$alert | set-scomalert -ResolutionState 255 -Comment $args[0]

}

import-module OperationsManager

$agent = Get-ScomAgent | Where {$_.PrincipalName –eq $computerPrincipalName}

$computer = $agent.HostComputer

if (!$agent) { fixalert “Resolved by MM-Script. MM not set!”; Exit }

$startTime = [DateTime]::Now

$endTime = $startTime.AddMinutes($Minutes)

$Instance = Get-SCOMClassInstance -Name $computerPrincipalName

if ($Instance -ne $null)

{

“$startTime : Setting MM for $computerPrincipalName – AlertId: $alertid ” | out-file “mm.log” -Append

try{

Start-SCOMMaintenanceMode -Instance $Instance -Reason “PlannedOther” -EndTime $endTime -Comment “Maintenace Mode Set by AutoMM”

Get-SCOMAlert -Id “$alertid” | Set-ScomAlert -ResolutionState 255 -Comment “Resolved by Auto Maintenance Mode Script”

}

catch

{

“$startTime : Error $error setting MM. ” | out-file “mm.log” -Append

}

}

else

{

“$startTime : Failed to find instance for $computerPrincipalName” | out-file “mm.log” -Append

}

 

 

Hope it helps!