Putting a computer into maintenance mode

Putting a computer into maintenance mode is what users do when they know a particular computer will be offline for a while (security update, known network issues, hardware installation, other things). This is done so that OpsMgr does not generate any alerts about this computer. The interesting part is that OpsMgr will still generate one particular alert. This has to do with the fact that the management server is not getting heart beats from the agent. In order to avoid the alert, you need to put the instances of HealthService and HealthServiceWatcher that are associated with that computer into maintenance mode as well.

 Here is a script that based on a computer name, number of hours to be in maintenance mode and a comment will put all the right pieces into maintenance mode:

 

param($computerPrincipalName,$numberOfHoursInMaintenanceMode,$comment)

$computerClass = get-monitoringclass -name:Microsoft.Windows.Computer

$healthServiceClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthService

$healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher

$computerCriteria = "PrincipalName='" + $computerPrincipalName + "'"

$computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteria

$healthServices = $computer.GetRelatedMonitoringObjects($healthServiceClass)

$healthService = $healthServices[0]

$healthServiceCriteria = "HealthServiceName='" + $computerPrincipalName + "'"

$healthServiceWatcher = get-monitoringobject -monitoringclass:$healthServiceWatcherClass -criteria:$healthServiceCriteria

$startTime = [System.DateTime]::Now

$endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode)

"Putting " + $computerPrincipalName + " into maintenance mode"

New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer -comment:$comment

"Putting the associated health service into maintenance mode"

New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthService -comment:$comment

"Putting the associated health service watcher into maintenance mode"

New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthServiceWatcher -comment:$comment

In order to run this script you will need to do the following:

1 - Save to a a file (C:\MaintenanceMode.ps1)

2 - Open up the OpsMgr Command Shell

3 - Type the following:

C:\MaintenanceMode.ps1 -computerPrincipalName:"dc.contoso.com" -numberOfHoursInMaintenanceMode:4 -comment:"test"

The computerPrincipalName should contain the FQDN of the computer (for computers that are domain members) which you wish to put into maintenance mode.