How to check SCOM Maintenance Mode history (version 2)?

Few months ago I added here simple script for checking SCOM maintenance mode history – link. I decided to adjust it a little bit and convert it into more advanced function.

To get SCOM maintenance mode history remotely for some specific server basically we can run just this command:

Invoke-Command -ErrorAction Stop -ComputerName SCOM01 -ScriptBlock{
Get-SCOMMaintenanceMode -Instance (Get-SCOMClassInstance -Class (Get-ScomClass -Name "Microsoft.Windows.Computer") | 
Where-Object {$_.DisplayName -match "DC01"}) -History -ErrorAction 0
} -Credential Get-Credential


Get-SCOMMaintenanceModeHistory -Computername DC01
Get-SCOMMaintenanceModeHistory -Computername DC01 -Verbose
Get-SCOMMaintenanceModeHistory -Computername DC01 -SCOMServer SCOM02
Get-SCOMMaintenanceModeHistory -Computername DC01 | Out-GridView
Get-SCOMMaintenanceModeHistory -Computername DC01,DCO2,DC03 | Format-Table
Get-SCOMMaintenanceModeHistory -Computername (gc "c:\temp\servers.txt")
Get-SCOMMaintenanceModeHistory -Computername DC01,DCO2,DC03 | Export-Csv -Path C:\users\$env:username\desktop\results.csv -NoTypeInformation 

Function will create an object for each server and add it to $Results array. Object will contains the following properties:

– Server
– StartTime
– EndTime
– ScheduledEndTime
– User
– Reason
– ManagementGroup
– Comments

Final script:

Function Get-SCOMMaintenanceModeHistory {
    # Parameters used in this function
        [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide server names", ValueFromPipeline = $true)] 
        [Parameter(Position=1, Mandatory = $False, HelpMessage="Select SCOM server", ValueFromPipeline = $true)][ValidateSet("SCOM01", "SCOM02", "SCOM03")][string]
        $SCOMServer = "SCOM1"
    $Results = @()
    $Credentials = Get-Credential "domain\pawel.janowicz"

    ForEach($Comp in $Computername)
        Write-Verbose -Message "Searching for $comp SCOM maintenance mode history"
        [string]$Comp = $Comp.trim()
            $MM = Invoke-Command -ErrorAction Stop -ComputerName $SCOMServer -ScriptBlock{
                Get-SCOMMaintenanceMode -Instance (Get-SCOMClassInstance -Class (Get-ScomClass -Name "Microsoft.Windows.Computer") | Where-Object {$_.DisplayName -match "$comp"}) -History -ErrorAction 0
            } -Credential $Credentials
            Write-Verbose -Message "No maintenance mode history has been found for $comp"
            Write-Verbose -Message "Maintenance mode history for $comp has been found"
            ForEach($item in $MM)
                $Object = New-Object PSObject -Property ([ordered]@{ 
                    Server                  = $Comp
                    StartTime               = $item.StartTime
                    EndTime                 = $item.EndTime
                    ScheduledEndTime        = $item.ScheduledEndTime
                    User                    = $item.User
                    Reason                  = $item.Reason
                    ManagementGroup         = $item.ManagementGroup
                    Comments                = $item.Comments
                $Results += $Object


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.