Freigeben über


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.

Comments

  • Anonymous
    October 23, 2007
    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
  • Anonymous
    February 29, 2008
    There are many articles that talk about maintenance mode in System Center Operations Manager 2007. Topics
  • Anonymous
    February 29, 2008
    There are many articles that talk about maintenance mode in System Center Operations Manager 2007. Topics
  • Anonymous
    August 21, 2008
    Check out Boris Yanushpolsky&#39;s blog ( http://blogs.msdn.com/boris_yanushpolsky ). Boris is an SCOM
  • Anonymous
    November 03, 2008
    You cannot call a method on a null-valued expression.I ran the script from its absolute path.Any help is appreciated.
  • Anonymous
    November 03, 2008
    $computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteriaFor 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-monitoringobjectRV
  • Anonymous
    November 19, 2008
    The Script for stopping maintenance modeparam($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:$healthServiceWatcherThe commandC:WINDOWSsystem32WindowsPowerShellv1.0powershell.exe C:MaintenanceModeStopMaintenanceMode.ps1 –rootMS: ‘localhost’ -computerPrincipalName: 'BRAMVICAP901.contoso.com'