Check SCOM Maintenance Mode history for multiple servers

Some time ago I posted short article about how to check SCOM Maintenance Mode history for single machine. Today I would like to share with you a little bit more advanced script which helps to check multiple servers.

SCOM Maintenance Mode history
SCOM History
SCOM History

To get above output we need to run the script on SCOM server. Remember that not all maintenance mode history information can be found there. It will show only the latest.

Final script:

Clear-Host
$Servers = Get-Content -Path "C:\temp\servers.txt"
$MMode = $null
$InstanceClass = $null
$Instance = $null
$Array = @()

$InstanceClass = Get-SCOMClass -Name "Microsoft.Windows.Computer" -ErrorAction Stop

Foreach($Server in $Servers)
{
    Write-Host "Checking $Server"
    Try
    { 
        $Instance = Get-SCOMClassInstance -Class $InstanceClass -ErrorAction Stop | Where-Object {$_.DisplayName -match $Server}         
        $MMode = (Get-SCOMMaintenanceMode -Instance $Instance -History -ErrorAction Stop)
    }
    Catch
    {
        $_.Exception.Message
        Continue
    }

    If($MMode)
    {
        Foreach($MM in $MMode)
        { 
            $Object = New-Object PSObject -Property ([ordered]@{ 
 
                Server                  = $Server
                StartTime               = $MM.StartTime
                EndTime                 = $MM.EndTime
                ScheduledEndTime        = $MM.ScheduledEndTime
                User                    = $MM.User
                Reason                  = $MM.Reason
                ManagementGroup         = $MM.ManagementGroup
                Comments                = $MM.Comments
 
            })  
            # Add custom object to our array
            $Array += $Object
        }
    }
    Else
    {
        Write-Warning "No maintenance mode history found for $Server"
    }
}

If($Array)
{
    $Array | Format-Table -Wrap -AutoSize
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.