OpsMgr 2012: Group Maintenance Mode via PowerShell (the way it should be)

You’ll have to forgive me as I’m going to go through a bit of history in order to explain what I mean by “the way it should be” in the title of this post. I’ve provided a sample script to demonstrate one way to implement this methodology in your environment.

The short version is this:

Group maintenance mode scripts should be just like computer maintenance mode, short and simple. Really, maintenance mode should be an operation we complete in the Operations Console, but as with any other company, time and resource constraints require Microsoft to make choices and set priorities.

In the time of Operations Manager 2007 RTM, SP1 and even R2, many people were going to a lot of unnecessary effort to put groups of objects into maintenance mode. The widely accepted methodology was:

1. Retrieve the group

2. Loop through the objects contained in the group

3. Put object into maintenance mode

4. Go to next object and repeat

This was not very efficient and resulted in a script making lots of calls to Operations Manager 2007 via the SDK.

Then, Steve Rachui (of Microsoft and general System Center renown) showed the world that all you needed to do was put the group object itself into maintenance mode and OpsMgr would take care of the contained objects for you! Basically, recursion (maintenance mode for “this object and contained objects” was assumed and handled for you. (in case you’re interested, that post was titled “Place a Group in Maintenance Mode with PowerShell”)

Fast Forward to OpsMgr 2012

Naturally, I quickly decided to duplicate Steve’s methodology in an OpsMgr 2012 version of the script, only to discover that the assumption of recursion was missing. Fortunately, this is not such a big deal if you delve into the SDK and look into the .ScheduleMaintenanceMode function, which allows for the inclusion of a request for recursion….the implementation of the “this object and contained objects”.

Here’s a sample script that implements group maintenance mode in a concise and efficient manner, with named parameters. The script is so short, the header with the instructions may be longer than the script itself!

Sample Script and Usage
Since this is a function, you need to call the function in your script as shown in the sample script below. This also eliminates the need to specify parameters to be passed into the script at the command line, making scheduling as a Windows Scheduled Task easy.

Syntax:
GroupMaintMode -ScomServer “mmsscom01” -GroupDisplayName “SQL Server 2008 Computers” `
-DurationInMin 10  -Reason “ApplicationInstallation” -Comment “Scheduled weekly maintenance”

Additional Resources

There are many sample System Center PowerShell scripts on System Center Central, including these:

Master Collection of System Center PowerShell

OpsMgr 2012 PowerShell: updated collection of one-liners

OpsMgr 2012: Running a Task in Bulk Using PowerShell

OpsMgr 2012: Automating Agent Discovery and Deployment with PowerShell [sample script]

OpsMgr 2012: Identifying Computers in Active Directory without an OpsMgr Agent Installed [sample script]

OpsMgr 2012 Quick Tip: Finding servers experiencing the most heartbeat failures with PowerShell

26 thoughts on “OpsMgr 2012: Group Maintenance Mode via PowerShell (the way it should be)

  1. joedali

    Guys – copying this script from the page leaves garbled data when pasting into notepad.

    Can you please package this up in a zip file for the minions who dont’ want to type it in?

    Thanks a ton.

  2. trishasalas

    curtmcgirt, mouse over the code window at the top and you’ll see a gray bar drop down.  Click on the <> icon (see screenshot) and you’ll have the option to view the raw code (without the numbered lines).
    code

    Sorry for the confusion, I’m going to be posting some mini-tutorials this weekend based on user-feedback.

    Hope this helps.

    -Trisha

  3. Tom Gerald

    I found the new script formatting very intuitive. I just copied and the unwanted line numbers didn’t come with. Agree a tutorial for others would be a good idea.

  4. Pingback: Agendar entrada e saida do Maintenance Mode (SCOM) » System Center Tips

  5. BTruter

    I have a strange issue with this script. I set it to run through the task manager, and it does indeed put the groups I specified into maintenance mode. Unfortunately, they do not come out of maintenance mode after the designated time, however.

    For testing, i set it for a 5 minute duration, and ran the task. I see in the OM console:

    Maintenance Mode Start Time:
    4/11/2013 9:00 AM

    Maintenance Mode End Time:
    4/11/2013 9:05 AM

    Maintenance Mode User:
    DOM\user

    Maintenance Mode Reason:
    Application: Maintenance (Planned)

    As of 9:30 am, they are still in maintenance mode.

    Any ideas why that would happen?

  6. Timothy Carroll

    I’m having the same problem as BTruter.  It seems when you set maintenance mode using the ScheduleMaintenanceMode method on the group object, it doesn’t always come out of maintenance mode.  It worked on one group of computers for me once, but now never seems to work right.

    On the bright side, Stefan’s script DOES work.  It uses the Start-MaintenanceMode cmdlet and I have not had an issue yet.  *knock on wood*

  7. Jonathan Danielewicz

    The only change I made was to remove the check for if the group was in maintenance. This puts it back to 2007 style where it updates the maintenance timeframe. We have several schedules stacked up over each other and it only worked if it updated the minutes.

     

  8. Jason

    The script above is great if you don’t have any maintenance mode already scheduled for objects in the group. If you do have maintenence scheduled that would have lasted longer than the group’s, then it get’s over written.

    Any comments on how to avoid this?

  9. Pete Zerger Post author

    Its all about context. This is intended for use as a scheduled task or as the back end of a SCSM Self-Service Portal or SCOrch runbook. If you’re already scheduling through another script or method, you don’t want to mix them….pick one way or the other.

  10. Wilson W.

    FYI, Maarten Goet’s website (www.authoringfriday) has a great solution to put groups of servers into Maintenance Mode via the SCSM Self-Service Portal and Orchestrator:

     

    http://www.authoringfriday.com/2013/02/03/system-center-2012extended-oipmaintenance-mode-power-2/

     

    It’s definitely worth checking out.

     

     

     

    On a separate note, I really find it hard to believe that after all this time Microsoft still hasn’t built the ability to schedule maintenance mode into SCOM.  I distinctly remember during MMS2011 they announced that SCOM2012 would have the ability to do this…..then nothing came of it.  Maintenance Mode scheduling has to be the #1 complaint of every SCOM admin and yet even after SP1 this ability is still not there…..I just can’t understand why they dropped the ball on this and why today we still need to rely on community-created solutions to address this need.

  11. Shaun Collins

    I looked at that option and instead used this script in a runbook to do the job b providing a static list of groups allowed for maintenance to ensure nobody could accidentally put all servers into maintenance mode.

  12. HarashukuGrl

    Wilson, I on see nothing about SCSM integration in the link you provided. Did you mean to share a different URL?

  13. HarashukuGrl

    That example uses a simple list, just as we do n SCSM using this script. Not much reason for us to change a this point, as this script in a runbook allows us to start or stop from a single runbook, using a sample runbook from the Opalis Unleashed book we updated for SCOrch.

  14. joedali

    Stefan’s script for the win, even though its a little buggy too.

    Why does Microsoft continue to lag with this important option?

    Hey, how about right click on a group and have a “Schedule Maintenance Mode” option.

    Too difficult I guess.

     

  15. sylvester

    BTW, In OpsMgr 2012 SP1 recursion was returned, but only for Start-SCOMMaintenanceMode cmdlet.

    Set-SCOMMaintenanceMode cmdlet still doesn’t support it.

  16. Pete Zerger Post author

    Due to that fact and reduced complexity overall is why I opted for Start-SCOMMaintenanceMode and ad-hoc, Windows Task Scheduler and Orchestrator-driven maintenance mode.

  17. alsoszaa

    I have a group of Cluster Resources that are dynamic (since we constantly add and delete VMs from our cluster). When I try to set Maintenance Mode on the group of dynamic objects (the only thing in the group) I receive this error. Is there a better way to set these into MM? The reason I am doing this is because when I set the watchers and the computer objects into MM first the cluster resourses send several alerts that they are failed (I am using the Failover Cluster Management Pack). We do not install agents on the VMs themselves. Just the Hosts. then the hosts have proxy enabled so that the cluster resources (like VMs) can communicate through to SCOM.

    PS C:\Users\administrator\desktop> .\Set-SCOMGroupMM3.ps1 -scomserver SCOMMS01 -GroupDisplayName Cluster1_VMs -DurationMin  30 -Reason UnplannedOther New-SCManagementGroupConnection : The requested name is valid, but no data of the requested type was found At C:\Users\administrator\desktop\Set-SCOMGroupMM3.ps1:39 char:34 + New-SCOMManagementGroupConnection <<<<  -ComputerName $ScomServer     + CategoryInfo          : ObjectNotFound: (Microsoft.Syste…nnectionCommand:NewSCManagementGroupConnectionCommand    ) [New-SCManagementGroupConnection], SocketException     + FullyQualifiedErrorId : ExecutionError,Microsoft.SystemCenter.Core.Commands.NewSCManagementGroupConnectionComman    d

  18. Oskar Landman

    Just to add

    I have written the Extended OIP and the reason why you cannot schedule it directly in SCOM is simply because there is no intelligence on the start method within the OpsMgr code / SDK. So yes this is missing in the product but up until now this is a fact.

    With Orchestrator you can put more intelligence behind it what makes it more robust on your scheduling an you more or less handle all intelligence in orchestrator.

    The examples on authoringfriday I have written just to show the power of this integration pack and how easily you can re-use this in far more advanced senario’s like using shared calendars to schedule MM – http://www.authoringfriday.com/2013/01/23/system-center-2012-extended-oip-scheduling-opsmgr-maintenance-mode-in-exchange-calendar/

    Like Pete stated it is all about context and how you want to use it. One would prefer PowerShell but requires some PowerShell knowledge and although it looks to be really flexible it isn’t /wasn’t always….You could only use the methods in your scripts presented in PowerShell.

    This is where I found limitations like the recursion and wanted to be able to experience the orchestrator way of creating runbooks without writing scripts and simply because at least these needed to be default in the standard integration pack. Therefore added all methods for MM in the activity. This is the same reason why I created the other activities as well to get a class and ALL it properties not just a subset…

    The funny part about the example I have shown by using a simple list on the portal is because the selection of groups in the SSP is limited by SCSM itself and I couldn’t add the groups to the selections as objects…still need to dive into that one… 😉

    My idea was to make it fully dynamic but this failed on the last part to add the groups to the service request offering template…. don’t know why it is not in there from a gui perspective may be able to pull this of by only using XML but need to test.

    If you feel you are missing options in the extended OIP please let me know and will look at “fixing” these.

     

    regards,

    Oskar Landman

     

    Agree this should be in the product but up until now still it isn’t… also besides this scheduling more intelligent would be to include a clever meganism not to put more than 50 % of you management servers into MM…

     

     

  19. Mike

    Hi Peter,

    What would be the best way to modify this script to end the maintenance mode?

    We currently use GroupMM.ps1 that has options for True “to enable Maintenance Mode” and False “to Exit maintenance mode”. Our apps team use this for weekly application deployments.

    Example: StartMM: powershell “\\SCOMServer\c$\scripts\groupmm.ps1” -groupName:’Group -Servers Group’ -hours:4 -rmsServerName:’SCOMServer’ -startMM:$true

    Example: StopMM: powershell “\\SCOMServer\c$\scripts\groupmm.ps1” -groupName:’Group -Servers Group’ -hours:4 -rmsServerName:’SCOMServer’ -startMM:$false

    I can’t find anything like it to work with SCOM 2012. I would even be happy enough if the script can be modified to update the existing maintenance window to the minimum 5min.

    Any help would be greatly appreciated

    Thanks,

    Mike

  20. Mike

    Hey all,

    I answered my question. Using the script below i was able to exit maintenance mode for a group. MM Group is the name of the Group in SCOM. Tested on SCOM 2012R2

    Import-Module OperationsManager
    $endtime = (Get-Date).ToUniversalTime()
    $Instance = Get-SCOMGroup -displayname “MM Group”
    $instance.stopmaintenancemode($endtime,”recursive”)

    We need a way to start and stop maintenance mode to a group of servers on damand for code deployments.

    The way it works for us is to use scheduled tasks and then give our dev and application guys access to remotley execute the scheduled task using:

    schtasks /Run /S SCOM-Server /I /TN “TASK NAME”

    Hope this helps

    Mike

     

     

  21. BGW

    I’ve been using this script for 2 years now with great success.  I have dozens of scheduled tasks calling variants of this script for each maintenance plan that I need to invoke for various groups.  However I just applied UR8 for 2012 R2, and the script no longer works.  ACK!  Someone please help.

  22. BGW

    Well, I’ve figured it out…  Something in UR8 for SCOM has changed the object/classes of group members.  To the for criteria “| Get-SCOMClassInstance” needs to be added so the whole line line looks like the below

     

    ForEach ($Group in (Get-ScomGroup -DisplayName  $GroupDisplayName | Get-SCOMClassInstance))

Leave a Reply

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