Some weeks ago, I wrote a blog post about the creation of SCOM groups in order to subscribe to alerts. Subscribe to alert is mandatory, of course, to be able to receive alerts concerning our group. But during operations like an update, a patching, …, we don’t want to be spoiled by lots of alerts. To avoid those unexpected Emails, we need to place our group and so, objects contained in this group, in maintenance mode.

I will use a PowerShell script to do this job.
The parameter of my script will be:

  • ManagementServer: mandatory parameter containing management server name
  • GroupName: mandatory parameter containing display name of the target group
  • DurationTimeMin: mandatory parameter containing the duration maintenance time in minutes
  • Comment: mandatory parameter containing a comment for the maintenance time
  • Reason: mandatory parameter containing the reason of the maintenance, value are predifined and should be: PlannedOther, UnplannedOther, PlannedHardwareMaintenance, UnplannedHardwareMaintenance, PlannedHardwareInstallation, UnplannedHardwareInstallation, PlannedOperatingSystemReconfiguration,     UnplannedOperatingSystemReconfiguration, PlannedApplicationMaintenance, ApplicationInstallation, ApplicationUnresponsive, ApplicationUnstable, SecurityIssue, LossOfNetworkConnectivity

In PowerShell:

param(
 [Parameter(Mandatory=$true)][string]
 $ManagementServer,
 [Parameter(Mandatory=$true)][string]
 $GroupName,
 [Parameter(Mandatory=$true)][int32]
 $DurationTimeMin,
 [Parameter(Mandatory=$true)][string]
 $Reason,
 [Parameter(Mandatory=$true)][string]
 $Comment
 )

I need to import the necessary module for SCOM:

# Import necessary module for SCOM
 Import-Module OperationsManager

I will now create a persistent connection to my Management Group:

# Create connection to SCOM Management Group
 New-SCOMManagementGroupConnection -ComputerName $ManagementServer

 I have now just to find my SCOM group with his name and to place it in maintenance mode for the duration period I specified before:

# Find group and place in maintenance mode
ForEach ($Group in (Get-ScomGroup -DisplayName $GroupName))
    {
   If ($group.InMaintenanceMode -eq $false)
         {
            $group.ScheduleMaintenanceMode([datetime]::Now.touniversaltime(), `
            ([datetime]::Now).addminutes($DurationTimeMin).touniversaltime(), `
 
             "$Reason", "$Comment" , "Recursive")
         }
    }

To run my script I open a PowerShell screen and execute the following command:

MaintenanceMode4

I go now to SCOM and check for my group. I see that my group contains two SQL Server instances:

MaintenanceMode2

MaintenanceMode3

Those two instances are now in maintenance mode:

MaintenanceMode1

This simple script will be very practical to place group in maintenance mode and I will use it in a future blog post to schedule with PowerShell maintenance mode for SCOM groups.
See you soon 😉