Get-ManagedFolderAssistantReport.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID ca5f8efd-05ef-47d9-a101-6a8c6419d911
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2021
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI https://www.undocumented-features.com/2021/02/03/reviewing-the-exchange-mailbox-diagnostics-log
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION
Use this script to generate a report of managed folder assistant runs from the mailbox diagnostic log.
 
.PRIVATEDATA
 
#>


<#
.SYNOPSIS
Report on the per-mailbox status of the Managed Folder Assitant.
 
.PARAMETER ConsoleOutput
Switch to enable console output.
 
.PARAMETER Identity
Specify an identity. If not specified, prompt.
 
.PARAMETER IncludeHtmlOutput
Switch to enable Html Output.
 
.PARAMETER Outfile
Report output file name.
 
.EXAMPLE
.\Get-ManagedFolderAssistantReport.ps1 -Identity john
Generates managed folder assistant report for mailbox 'john'.
 
Get-Mailbox | .\Get-ManagedFolderAssistantReport
Generates managed folder assistant report for all mailboxes.
 
#>

[CmdletBinding()]
param (
    [switch]$ConsoleOutput,
    [Parameter(Mandatory = $True, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, Position = 1)]$Identity,
    [switch]$IncludeHtmlOutput,
    [string]$OutFile = (Get-Date -Format yyyy-MM-dd) + "_ManagedFolderAssistantReport.csv"
)

begin
{
    # Check parameters
    If ($PSBoundParameters.ContainsKey('Identity') -and $MyInvocation.PipelineLength -eq 1)
    {
        [object[]]$Identity = Get-Mailbox $Identity
    }
    Else
    {
        [object[]]$Identity = Get-Mailbox -ResultSize Unlimited
    }
    $global:Report = @()
    
    $Total = $Identity.Count
    $i = 1    
}

process
{
    # Select Mailbox Identity Data
    If ($PSBoundParameters.ContainsKey('Identity') -and $MyInvocation.PipelineLength -eq 1)
    {
        $DisplayName = $Identity.Name
        $Alias = $Identity.Alias
        $PrimarySmtp = $Identity.PrimarySmtpAddress
    }
    Else
    {
        $DisplayName = $_.Name
        $Alias = $_.Alias
        $PrimarySmtp = $_.PrimarySmtpAddress
    }
    
    Write-Progress -Activity "Gathering mailbox diagnostic data" -Percent (($i/$Total) * 100) -CurrentOperation "Processing $($DisplayName)"
    $ElcLastSuccessTimeStamp = $null
    $ElcLastRunDeletedFromRootItemCount = $null
    [xml]$MailboxLog = (Export-MailboxDiagnosticLogs -Identity $Identity.Alias -ExtendedProperties).MailboxLog
    $ElcLastSuccessTimeStamp = ($MailboxLog.Properties.MailboxTable.Property | ? { $_.Name -like "*ElcLastSuccessTimeStamp*" }).Value
    $ElcLastRunDeletedFromRootItemCount = ($MailboxLog.Properties.MailboxTable.Property | ? { $_.Name -like "*ElcLastRunDeletedFromRootItemCount*" }).Value
    If ($ElcLastSuccessTimeStamp -eq $null) { $ElcLastSuccessTimeStamp = "Never run" }
    If ($ElcLastRunDeletedFromRootItemCount -eq $null) { $ElcLastRunDeletedFromRootItemCount = "N/A" }
    $Entry = [PSCustomObject]@{
        User           = $DisplayName
        LastProcessed  = $ElcLastSuccessTimeStamp
        ItemsProcessed = $ElcLastRunDeletedFromRootItemCount
    }
    $global:Report += $Entry
    If ($ConsoleOutput) { $Entry }
    $i++
}

end
{
    $Report | Export-Csv -NoTypeInformation $OutFile
    If ($IncludeHtmlOutput) { $Report | ConvertTo-Html | Out-File $($Outfile).Replace(".csv","html.")}
    Write-Host "To review the report in the console, type `$Report and press return."
}