Private/Connect-ExchangeService.ps1

# Connect-ExchangeService.ps1
# Authenticates to Exchange Online using the established Graph context.
# Part of the M365-QuickAssess module -- not exported.

function Connect-ExchangeService
{
    Write-Log "Connecting to Exchange Online..."

    if ( [string]::IsNullOrEmpty( $script:Context.GraphAccount ) )
    {
        throw "Connect-ExchangeService: GraphAccount is not set in context. Ensure Connect-GraphService ran successfully."
    }

    if ( [string]::IsNullOrEmpty( $script:Context.TenantId ) )
    {
        throw "Connect-ExchangeService: TenantId is not set in context. Ensure Connect-GraphService ran successfully."
    }

    try
    {
        Connect-ExchangeOnline `
            -Organization      $script:Context.TenantId `
            -ShowBanner:$false `
            -ErrorAction Stop

        Write-Log "Connected to Exchange Online"
    }
    catch
    {
        Write-Log "Exchange Online connection failed: $( $_.Exception.Message )" "ERROR"
        throw
    }
}