Set-ScomMaint

Here is a little function I put together to QUICKLY be able to place servers in SCOM maintenance. For anyone who uses the GUI to do this, you know it can get a little clunky. This saves me loads of time. I sourced some of the parts of this script from various sites that I no longer remember. Please forgive not giving credit to those particular sections.

You must change the following variables to entries from your environment.
$ManServer = “Man.Server.FQDN”
$domain = “domain.FQDN.com”


<#
    .SYNOPSIS
        The Set-SCOMMaint function places a server or servers in
                maintenance mode programatically.
    .DESCRIPTION
        This function will place a server or servers in maintenance
                mode programitically with little to no error checking.
                The management server is hard coded into this version of the
                function.
    .PARAMETER  computers
        computers is used to define which servers to enter into maintenance.
                a single machine name can be entered, or a comma seperated value of
                machine names can be entered.
    .PARAMETER  minutes
        minutes is used to define the amount of time in minutes to set maintenance mode for.
    .EXAMPLE
        Set-SCOMMaint -computername bobsPC -minutes 10
    .EXAMPLE
        Set-SCOMMaint -computername bobsPC,BillsPC,BensPC -minutes 240
    .version beta
                First release.  Feature add to come.  Stay tuned.
#>



Function Get-LocalTime($UTCTime)
{
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ)
Return $LocalTime
}



Function Set-SCOMMaint
{
        Param
        (
        [array]$computers,
        [string]$minutes
        )

# Defining global variables
$ManServer = "server.fqdn.jameier.com"
$domain = "jameier.com"
$class = "Microsoft.Windows.Computer"
$comment = 'Scheduled Maintenance'

# Adding temporary Environment variable
$p = [Environment]::GetEnvironmentVariable(“PSModulePath”)
$p += “;C:\Program Files\System Center 2012\Operations Manager\Powershell\”
[Environment]::SetEnvironmentVariable(“PSModulePath”,$p)

# Importing necessary modules
Import-Module OperationsManager

# Connect to management server, and get\set times
New-SCOMManagementGroupConnection $ManServer
$class = Get-SCOMClass -Name $class
$startTime = [System.DateTime]::Now.ToUniversalTime()
$endTime = [System.DateTime]::Now.AddMinutes($minutes).ToUniversalTime()

# Begin loop
foreach ($computer in $Computers) {
 $instance = Get-SCOMClassInstance -Class $class | ? { $_.Name -eq ($computer + "." + $domain) }
 Write-Host -foregroundcolor yellow Attempting to place $computer into maintenance mode for $minutes minutes
 $instance.ScheduleMaintenanceMode($startTime, $endTime, "PlannedApplicationMaintenance", $comment , "Recursive")
 $InMaint = Get-SCOMMaintenanceMode -Instance $Instance
 $InMaintLocalTime = Get-LocalTime($InMaint.ScheduledEndTime)
 Write-Host -foregroundcolor Green $computer entered into maintenance mode until $InMaintLocalTime
 Start-Sleep -s 1
}
}