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

Usage:

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 {
    [CmdletBinding()]
               
    # Parameters used in this function
    Param
    (
        [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide server names", ValueFromPipeline = $true)] 
        $Computername,
 
        [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()
        Try
        {
            $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
        }
        Catch
        {
            $_.Exception.Message
            Continue
        }
         
        If(!$MM)
        {
            Write-Verbose -Message "No maintenance mode history has been found for $comp"
        }
        Else
        {
            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

            }
        }
    }
 
        If($Results)
        {
            $Results
        }
}

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.