How to setup scheduled Maintenance Mode for servers in a SCOM group.

Firstly, I’d like to thank Søren Kjærhus for the help in getting this up & running (http://www.systemcentercentral.com/forums-archive/topic/scom-2012-automate-server-maintenance-mode/#post-215813).

 

My scenario:

Our Production Support team starts their shift at 5am every morning and what they do for the next hour is restart important services, web portals, applications, etc, on a number of very important servers. The problem with this is that from 5am to 6am every Mon to Fri, we get a bunch of SCOM alerts about servers rebooting and portals going down and clusters failing, etc. It looks a bit messy and is not indicative of what’s really happening in the environment.

So my goal was to setup these specific servers to go automatically into Maintenance Mode every Mon to Fri from 5am to 6am.

There are three steps in setting this up:

  1. Obtain the list of servers and put them into a SCOM group. My group is called “MyCompany- Morning Check Servers Group”. I placed the computer objects of each computer in this group, which would take care of all properties pertaining to the servers, but you could get granular and, say, add just the SQL objects of each computer so that only the SQL components go into Maintenance Mode while the rest of the server items are still being monitored.
  2. Create the PowerShell script which will serve as the interface or engine which drives the Maintenance Mode action. My script accepts variables so it can be used generically and I have a specific CMD script file which runs the Maintenance Mode PowerShell script using specific parameters.
  3. Create a Task Manager item on any server and run the CMD command.

 

So here is how to set it up.

 

1. Put the computer objects into a group.

Create a new group and include the computer objects which you want to place into Maintenance Mode. My group is called “MyCompany- Morning Check Servers Group”.

 

2. Create the PowerShell script.

The script was originally hardcoded with specific values but what I’ve done is added command-line parameters to it and made it as generic as possible so you can run the one script with different parameters.

These are the parameters we need to drive the script which does the brunt of the work:

 

Parameter Description:

-$ScomServer: mandatory parameter containing mgmt server name (Netbios or FQDN)
-$GroupDisplayName: mandatory parameter containing display name of the target group
-$DurationInMin: mandatory parameter containing integer of desired duration in minutes
-$Reason: mandatory parameter containing reason. Acceptable values are UnplannedOther,
           PlannedHardwareMaintenance,
           UnplannedHardwareMaintenance,
           PlannedHardwareInstallation,
           UnplannedHardwareInstallation,
           PlannedOperatingSystemReconfiguration,
           UnplannedOperatingSystemReconfiguration,
           PlannedApplicationMaintenance,
           ApplicationInstallation,
           ApplicationUnresponsive,
           ApplicationUnstable,
           SecurityIssue,
           LossOfNetworkConnectivity
-$Comment: optional parameter with free text description of your choice

 

Since the code is a function, you need to call the function in order to invoke it, using the last line in the script:

 

This line calls the function and passes the command-line parameters to it. Nice and simple!

You need to note the order of the code in this script. You need to make sure that the command-line parameters are defined first, then any functions and lastly you call the functions. I suppose PowerShell loads the script code and processes it from the first line down to the last and this is the way you need to think when coding PowerShell. Note that I am a PowerShell newbie so it’s all new to me!

 

3. Create the Task Manager item.

In order to run a PowerShell script from the Task Manager, you should call it from a CMD script first. What I have done is created multiple CMD files for each group of servers I want to put in Maintenance Mode (I have many scenarios like WSUS updates, Firmware updates, etc, which involves different servers). For example, when Production Support does their thing from 5am to 6am on the weekdays, I have one CMD script. For WSUS updates to servers I have another CMD script, etc. Each CMD script contains different parameters which it feeds to the PowerShell script and this is why I’ve modified the PowerShell script to be universal.

So here’s my CMD script to put servers into Maintenance Mode each weekday from 5am to 6am:

The CMD code is very simple. It calls the PowerShell script and feeds the script specific parameters.

 

Now we need to setup a new Windows task and call the CMD file. This is required because you cannot schedule maintenance mode in SCOM.

Create a task such as the following:

General- Edited 

 

Setup a trigger so that the PowerShell script can run at a predefined date & time:

Triggers

As you can see, my trigger runs every weekday at 5am.

 

Now we need to define the command to run. Since my PowerShell script is purposely generic, it accepts command-line parameters which enables me to use the script in many different situations. For each situation, I have create a CMD batch/script file which runs the PowerShell environment & the PowerShell script along with the required command-line parameters. Here it is in the Task Manager’s Action tab:

Actions

In my opinion, it’s good to specify the Start In parameter when working with scripts.

 

The rest of the task setup is up to you to configure as you like. Now, when you run the task, whether it be manually or by the schedule, your computer objects will go into maintenance mode for the specified time.

 

That’s it, you’re done!

 

PowerShell script code:

 

Here’s my PowerShell script, called “SetGroupMaintenanceMode.ps1”

 

As you can see this is an existing script (created by Pete and the original is found here: http://www.systemcentercentral.com/opsmgr-2012-group-maintenance-mode-via-powershell-the-way-it-should-be/) which I have modified. It now accepts command line parameters to make the script more generic so it can be used for multiple scenarios. So just create one CMD batch file for each scenario feeding different command-line parameters to the PowerShell script.

3 thoughts on “How to setup scheduled Maintenance Mode for servers in a SCOM group.

  1. Wilson W.

    A quick thought……..I always dislike having things run in task scheduler since you have very little visibility if the task fails to run for some reason.   It would be better to have Orchestrator manage and schedule the task because then you have runbook workflow visibility.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.