Script to put a group into maintenance mode

Create a group with your servers in it, if you are dynamically adding the servers into the group then as a safeguard add your SCOM management servers to the Excluded Members tab

If you like you can set this script to run as a scheduled task using the trigger powershell.exe “c:\scripts\myscript.ps1”

# Puts all servers in a specified group into maintenance mode
# Uncomment the ‘param’ line below if you want to pass values by parameter to this script
#param ($groupName, $MMDurationInHours, $rmsServerName)

#NOTE change the parameters below to suit your environment
$MMDurationInHours = “2.5”
$rmsServerName = “”
$groupName = “My Server Group”
$comment = “Routine maintenance mode procedure” # this comment shows up in the SCOM Console.
#Load the Operations Manager snapin and connect to the Root Management Server
add-pssnapin “Microsoft.EnterpriseManagement.OperationsManager.Client”;
Set-Location “OperationsManagerMonitoring::”;

$mgConn = New-ManagementGroupConnection -connectionString:$rmsServerName;
if($mgConn -eq $null)
[String]::Format(“Failed to connect to RMS on ‘{0}'”,$rmsServerName);

Set-Location $rmsServerName;

$startTime = [DateTime]::Now
$endTime = $startTime.AddHours($MMDurationInHours)
$MonitoringClassCG = get-monitoringclass | where {$_.DisplayName -eq $groupName}
$MonitoringGUID = get-monitoringobject $MonitoringClassCG.Id
New-MaintenanceWindow -startTime:$startTime -endTime:$endTime  -comment:$comment -monitoringObject:$MonitoringGUID



This entry was posted in Misc Stuff and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s