Stopping maintenance mode

I wrote a blog post several weeks ago about putting a computer into maintenance mode using PowerShell. For some reason after posting the script there was a white space infront of one of the parameters which resulted in the health service watcher object not being put into maintenance mode. I just fixed it and updated the post.

In the last week I got questions from several people on how to stop maintenance mode for a computer using PowerShell. Here is a script that I put together that does the job:

param($computerPrincipalName)

$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
 

"Stopping maintenance mode for " + $computerPrincipalName
$computer.StopMaintenanceMode([System.DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

"Stopping maintenance mode for the associated health service"
$healthService.StopMaintenanceMode([System.DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

"Stopping maintenance mode for the associated health service watcher"
$healthServiceWatcher.StopMaintenanceMode([System.DateTime]::Now.ToUniversalTime(),[Microsoft.EnterpriseManagement.Common.TraversalDepth]::Recursive);

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

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

2 - Open up the OpsMgr Command Shell

3 - Type the following:

C:\StopMaintenanceMode.ps1 -computerPrincipalName:"dc.contoso.com"

 

The computerPrincipalName should contain the FQDN of the computer.