Workloads/Get-ExchangeUsage.ps1

# Get-ExchangeUsage.ps1
# Collects 90-day mailbox activity via the Graph Reports API.
# Part of the M365-QuickAssess module -- not exported.

function Get-ExchangeUsage
{
    param
    (
        $Assessment
    )

    Write-Log "Collecting Exchange activity data (90 days)"

    try
    {
        $ex = Get-GraphReportCsv `
            -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D90')"

        if ( -not $ex -or $ex.Count -eq 0 )
        {
            Write-Log "Exchange activity report returned no data" "WARN"
            $Assessment.Exchange.ActiveMailboxCount = $null
            return
        }

        $activeCount   = ( $ex | Where-Object { $_."Last Activity Date" } ).Count
        $inactiveCount = $ex.Count - $activeCount

        $Assessment.Exchange.ActiveMailboxCount = $activeCount

        Write-Log "Exchange (90 days): Active=$activeCount Inactive=$inactiveCount"

        # -------------------------------------------------------------------
        # Finding: High inactive mailbox count
        # -------------------------------------------------------------------
        if ( $ex.Count -gt 0 )
        {
            $inactivePercent = [math]::Round( ( $inactiveCount / $ex.Count ) * 100, 0 )

            if ( $inactivePercent -ge 25 )
            {
                $Assessment.Findings += New-Finding `
                    -Type           "HighInactiveMailboxCount" `
                    -Summary        "$inactiveCount mailboxes have had no activity in the last 90 days ($inactivePercent%)" `
                    -Category       "Exchange" `
                    -Severity       "Medium" `
                    -Impact         "A high number of inactive mailboxes may indicate stale accounts consuming licenses." `
                    -Recommendation "Review inactive mailboxes before migration and deprovision or convert to shared as appropriate."
            }
        }
    }
    catch
    {
        Write-Log "Exchange activity collection failed: $( $_.Exception.Message )" "ERROR"
    }
}