Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Thursday, August 30, 2007 7:52 PM by Boris Yanushpolsky
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Stopping maintenance mode

Wednesday, October 24, 2007 3:27 AM by Wojtek Gurgul

Hi,

I was trying to run your script and I'm getting the errors like:

Method invocation failed because [System.Object[]] doesn't contain a method named

'GetRelatedMonitoringObjects'.

At C:\MaintenanceMode-test.ps1:24 char:56

+ $healthServices = $computer.GetRelatedMonitoringObjects( <<<< $healthServiceClass)

Does the script run for SCOM 2007 or MOM 2005 only? What may be a problem?

Wojtek

# Things to know about Maintenance Mode in System Center Operations Manager 2007

Friday, February 29, 2008 9:45 PM by SCOM/MOM

There are many articles that talk about maintenance mode in System Center Operations Manager 2007. Topics

# Things to know about Maintenance Mode in System Center Operations Manager 2007

Friday, February 29, 2008 9:49 PM by SCOM/MOM

There are many articles that talk about maintenance mode in System Center Operations Manager 2007. Topics

# A Great place for SCOM scripts

Thursday, August 21, 2008 12:40 PM by Dario IT Solutions Blog

Check out Boris Yanushpolsky&#39;s blog ( http://blogs.msdn.com/boris_yanushpolsky ). Boris is an SCOM

# re: Stopping maintenance mode

Monday, November 03, 2008 1:51 PM by RV

You cannot call a method on a null-valued expression.

I ran the script from its absolute path.

Any help is appreciated.

# re: Stopping maintenance mode

Monday, November 03, 2008 1:54 PM by RV

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

For some reason the value of $compter is Null.

That is the reason it is not able to access the method to stop the maintenane mode.

I have valid values for $computerClass  and also $computerCriteria but still have a  null Value being returned by get-monitoringobject

RV

# re: Stopping maintenance mode

Wednesday, November 19, 2008 10:24 AM by Christopher Keyaert

The Script for stopping maintenance mode

************************************

param($rootMS,$computerPrincipalName)

Add-PSSnapin "Microsoft.EnterpriseManagement.OperationsManager.Client" -ErrorVariable errSnapin;

Set-Location "OperationsManagerMonitoring::" -ErrorVariable errSnapin;

new-managementGroupConnection -ConnectionString:$rootMS -ErrorVariable errSnapin;

set-location $rootMS -ErrorVariable errSnapin;

$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

$endTime = [System.DateTime]::Now

"Stopping " + $computerPrincipalName + " maintenance mode"

Set-MaintenanceWindow -endTime:$endTime -monitoringObject:$computer

"Stopping the associated health service maintenance mode"

Set-MaintenanceWindow -endTime:$endTime -monitoringObject:$healthService

"Stopping the associated health service watcher maintenance mode"

Set-MaintenanceWindow -endTime:$endTime -monitoringObject:$healthServiceWatcher

************************************

The command

*********************************

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe C:\MaintenanceMode\StopMaintenanceMode.ps1 –rootMS: ‘localhost’ -computerPrincipalName: 'BRAMVICAP901.contoso.com'

*********************************

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker