OpsMgr 2007: How to get the list of all the computers in Maintenance Mode

Here's a cool tip from Ritesh Grover, one of our top support engineers on our Operations Manager team.  If you're looking for a script to quickly get a list of the computers that are in Maintenance Mode then this should do the trick:

========

To get a list of all computers and components that are in Maintenance Mode, copy and paste the below script in .ps1 format and run the script from an OpsMgr 2007 PowerShell console.  If a specific component is not in Maintenance Mode then it will give you an error for that component:

***************************Script Starts****************************
$Group='All Computers'

$groupObject = get-monitoringobject | where {$_.DisplayName -eq $group};
$groupagents = $groupObject.getrelatedmonitoringobjects()

foreach ($agent in $groupAgents)

{

$computerPrincipalName = $agent.displayname

$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
Write-host "****************************"
$agent.displayname
Write-host "****************************"
Write-host " "

Write-host "Computer"
Write-host " "
Get-MaintenanceWindow -monitoringobject:$Computer

Write-host "Health Service"
Write-host " "
Get-MaintenanceWindow -monitoringobject:$HealthService

Write-host "HealthServiceWatcher"
Write-host " "
Get-MaintenanceWindow -monitoringobject:$healthservicewatcher

}

***********************************END Script**************************************

Sample Output:

****************************
ComputerName.DomainSMS.com
****************************

Computer

Get-MaintenanceWindow : The MonitoringObject is not in maintenance mode.
At C:\mw.ps1:37 char:22
+ Get-MaintenanceWindow <<<< -monitoringobject:$Computer

Health Service

Get-MaintenanceWindow : The MonitoringObject is not in maintenance mode.
At C:\mw.ps1:41 char:22
+ Get-MaintenanceWindow <<<< -monitoringobject:$HealthService

HealthServiceWatcher

MonitoringObjectId : 81c07224-ea63-3206-937f-77d1efc29c86
StartTime : 10/14/2008 11:40:40 PM
ScheduledEndTime : 10/15/2008 12:10:40 AM
EndTime :
Reason : UnplannedOther
Comments :
User : DOMAINSMS\OpsAdmin
LastModified : 10/14/2008 11:40:40 PM
ManagementGroup : MGLab
ManagementGroupId : 3c3c8575-97dc-4e54-b4cc-1fffecffb7bf

So for the computer ComputerName in the above example output, Computer and Health Service error out but  HealthServiceWatcher gave valid data as only HealthServiceWatcher was in Maintenance Mode.

Note that the script above will check all the computers.  If you want to check for specific computers replace the $Group variable value with the group you wish to check.

========

Thanks Ritesh!

J.C. Hornbeck | Manageability Knowledge Engineer