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 (7)

  1. Wojtek Gurgul says:

    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

  2. SCOM/MOM says:

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

  3. SCOM/MOM says:

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

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

  5. RV says:

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

    I ran the script from its absolute path.

    Any help is appreciated.

  6. RV says:

    $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

  7. 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:WINDOWSsystem32WindowsPowerShellv1.0powershell.exe C:MaintenanceModeStopMaintenanceMode.ps1 –rootMS: ‘localhost’ -computerPrincipalName: ‘BRAMVICAP901.contoso.com’


Skip to main content