How to put servers in SCOM Maintenance Mode?

As some of you already know SCOM 2016 finally have a new feature called – Maintenance Schedules. It allows us to plan Maintenance Mode for several computer objects using GUI:

For those who didn’t migrate yet their SCOM infrastructures to 2016 version it might be hard to schedule maintenance mode during planned activities like weekend patching. I also had this problem so I developed a script and added it to task scheduler on SCOM server.

SCOM Maintenance Mode

First thing to do is to find out which SCOM Class we have to use. We can check it using Get-SCOMClass command – in this example class is called Microsoft.Windows.Computer. Then we need to specify for how long we want to keep servers in Maintenance Mode in my example it will be 48 hours from moment when script will be executed:

$Time = ((Get-Date).AddHours(48))

Input file where we can add servers for MM:

$Servers = Get-Content “D:\Scripts\input.txt”

We have to find an instance (server) which will be placed in MM:

$Instance = Get-SCOMClassInstance -Class $instanceclass | Where-Object {$_.DisplayName -match $Server}

To start maintenance mode we have to execute following command:

Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment “SCCM Patching” -Reason “PlannedOther”

In comment we can add description for MM and for reason we have to choose one of the following values.

Reason:

– PlannedOther
– UnplannedOther
– PlannedHardwareMaintenance
– UnplannedHardwareMaintenance
– PlannedHardwareInstallation
– UnplannedHardwareInstallation
– PlannedOperatingSystemReconfiguration
– UnplannedOperatingSystemReconfiguration
– PlannedApplicationMaintenance
– ApplicationInstallation
– ApplicationUnresponsive
– ApplicationUnstable
– SecurityIssue
– LossOfNetworkConnectivity

I also added command in # comments for checking if MM has been set correctly:

Get-SCOMMaintenanceMode -Instance $Instance | Select-Object ManagementGroup,User,StartTime,ScheduledEndTime,Rflnts | Format-List

Put servers in Maintenance Mode:

	
$instanceclass= Get-SCOMClass -Name Microsoft.Windows.Computer
$ErrorActionPreference = 'SilentlyContinue'
$Servers = Get-Content "D:\Scripts\input.txt" 
$Time = ((Get-Date).AddHours(48))

Foreach($Server in $Servers){
    $Instance = Get-SCOMClassInstance -Class $instanceclass | Where-Object {$_.DisplayName -match $Server}
    Start-SCOMMaintenanceMode -Instance $Instance -EndTime $Time -Comment "SCCM Patching" -Reason "PlannedOther"

        #Checking Maintenance Mode
        #$Server
        #Get-SCOMMaintenanceMode -Instance $Instance | select ManagementGroup,User,StartTime,ScheduledEndTime,Reason,Comments | fl
}
	

A little bit more advanced script 😀 :

$InstanceClass= Get-SCOMClass -Name Microsoft.Windows.Computer
$ErrorActionPreference = 'SilentlyContinue'
$Servers = Get-Content "c:\users\$env:username\desktop\servers.txt"
$ts = New-TimeSpan  -Hours 48 -Minutes 15
$Time = ((Get-Date) + $ts)
$Array = @()

Foreach($server in $Servers){
    $Server = $Server.trim()
    Write-Host "Processing $Server" -ForegroundColor Green

    $Mm = $Start = $End = $MMUser = $Reason = $MgmtGroup = $Comment = $Object = $FQDN = $Instance = $null
    
    $FQDN = ([System.Net.Dns]::GetHostByName(("$Server")))
    If(!$FQDN){
        Write-Warning "$Server does not exist"
    }
    Else{
        $Hostname = ($FQDN.HostName | Out-String).Trim()
        $Instance = Get-SCOMClassInstance -Class $InstanceClass | Where-Object {$_.DisplayName -match $Server}
    
        If(!$Instance){
            Write-Warning "$Server not found"
        }
        Else{
            Try{
                Start-SCOMMaintenanceMode -ErrorAction Stop -Instance $Instance -EndTime $Time -Comment "ESO8maintenance" -Reason "SecurityIssue"
                $Mm = Get-SCOMMaintenanceMode -ErrorAction Stop -Instance $Instance | Select-Object ManagementGroup,User,StartTime,ScheduledEndTime,Reason,Comments
                
                $Start = $MM.StartTime
                $End = $MM.ScheduledEndTime
                $MMUser = $MM.User
                $Reason = $MM.Reason
                $MgmtGroup = $MM.ManagementGroup
                $Comment = $MM.Comments
            }
            Catch{
                $_.Exception.Message
                
                $Start = "(null)"
                $End = "(null)"
                $MMUser = "(null)"
                $Reason = "(null)"
                $MgmtGroup = "(null)"
                $Comment = "(null)"
            }

            $Object = New-Object PSObject -Property ([ordered]@{ 
 
                Server                  = $Hostname
                StartTime               = $Start
                ScheduledEndTime        = $End
                User                    = $MMUser
                Reason                  = $Reason
                ManagementGroup         = $MgmtGroup
                Comments                = $Comment
 
            })
    
            $Array += $Object  
            #$Object 

            Remove-Variable Start,End,MMUser,Reason,MgmtGroup,Comment,MM,Object,FQDN,hostname,instance
        }
    }
} #Foreach end

#Export to CSV
$Array |  Export-Csv -Path C:\users\$env:username\desktop\MM.csv -NoTypeInformation

In our other article you can find out how to check SCOM Maintenance Mode history – link.
Check SCOM Maintenance Mode history on multiple servers – link.

Leave a Reply

Your email address will not be published. Required fields are marked *

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