I ran into an issue today where a important production server was not being monitored. As well- when the user placed the object into maintenance mode, they did not include comments. Lastly- the UI does not show who performed the action by default without digging.
So- today, I present to you, a simple powershell function to find all Windows Computers in maintenance mode, and to output a table showing the computer’s name, who placed it into maintenance mode, the start and end time, and comments if available.
Here is the simple script:
function Get-ObjectsInMaintenance
{
Param(
[Parameter(Mandatory=$false)][string[]]$excludeFromAccount = $null,
[Parameter(Mandatory=$false)][string]$ManagementPackClass = "Microsoft.Windows.Computer"
)
$class = Get-SCOMClass -Name $ManagementPackClass
$ComputersInMaintenance = Get-SCOMClassInstance -Class $class | Where InMaintenanceMode -eq $true
ForEach($com in $ComputersInMaintenance)
{
$mm = $com | Get-SCOMMaintenanceMode
#If there are values in excludeFromAccount, match the values against the User field.
if($excludeFromAccount -ne $null -and $excludeFromAccount -contains $mm.User)
{
continue
}
[pscustomobject]@{
Name = $com.Name
Who = $mm.User
StartTime = $mm.StartTime
EndTime = $mm.ScheduledEndTime
Comments = $mm.Comments
} | Write-Output
}
}
#Connect to your management group
New-SCOMManagementGroupConnection scom.mydomain.com
#Example 1. Show all Windows Computers in Maintenance Mode.
Get-ObjectsInMaintenance | Format-Table
##Example 2. Show all Windows Computers in Maintenance Mode, EXCEPT, if my account performed the action.
Get-ObjectsInMaintenance -excludeFromAccount 'MyDomain\AccountToExclude1' | Format-Table
##Example 3. Show all Windows Computers in Maintenance Mode, EXCEPT, by multiple accounts
Get-ObjectsInMaintenance -excludeFromAccount 'MyDomain\AccountToExclude1','MyDomain\AccountToExclude2' | Format-Table
#Example 4. Show all Unix objects in Maintenance Mode
Get-ObjectsInMaintenance -ManagementPackClass "Microsoft.Unix.Computer" | Format-Table
For the above script, it is capable of filtering out any number of potential accounts, which is handy if you don’t want to audit yourself, or a trusted service-account.
As well- while it defaults to microsoft.windows.computer, you can specify any separate management pack class to view.
Lastly- the above script does not include any error handling to validate you provided a valid class, or you have a valid connection… etc. Just sharing my simple cmdlet for anybody who may benefit from it.