SCOM 2007 R2 – Get-MaintenanceWindow PowerShell Commandlet and Suppressing the Errors

This can be a frustrating little commandlet. One would think that you could simply run the commandlet and get the list of objects current in MM:

Nope. It is a little more cumbersome than that. A majority of the time when dealing with MM you are probably dropping the Windows Computer objects into MM for something like patching. Since that’s a very common scenario, let’s just focus on getting all of the Windows Computer objects that are in MM. In my environment, my Orchestrator machine (aptly named SCORCH) is currently in MM but none of the others are.

Does it work? Sort of…

Tons of red. We do, however, see a single machine towards the end of the list that is in MM. Unfortunately, the commandlet doesn’t handle machines not in MM very well (or at all really). How can we clean this up?

And then:

Ideal? Maybe not…but it works!

$class = get-monitoringclass -name:Microsoft.Windows.Computer
$computers = get-monitoringobject -monitoringclass:$class
$ErrorActionPreference = ‘SilentlyContinue’
$computers | foreach{ get-maintenancewindow -monitoringobject:$_ }
$ErrorActionPreference = ‘Continue’

But that seems kind of cumbersome to have to manage the ErrorActionPreference by hand. What if I don’t want to have to try and remember to set the error action preference back? Well, you know that <common parameters> option each cmdlet seems to have?

Turns out one of those common parameters is the erroraction. You can do the suppression like this:

Saves a couple lines of code and doesn’t leave anything to clean up.

$class = get-monitoringclass -name:Microsoft.Windows.Computer
$computers = get-monitoringobject -monitoringclass:$class
$computers | foreach{ get-maintenancewindow -monitoringobject:$_ -ea:SilentlyContinue}

Leave a Reply