Public/Connect-CustomerExchange.ps1

function Connect-CustomerExchange {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$CustomerTenantId
    )
    # Get SAM tokens if not already available
    begin {
        if (!$SAMTokens) {
            $SAMTokens = Get-SAMTokens
        }  
    }
    
    # Generate a token for MS graph, and connect with it.
    process {
        try {
            $exchangeToken = New-PartnerAccessToken -ApplicationId $SAMTokens.ApplicationId -Credential $SAMTokens.ApplicationCredential -RefreshToken $SAMTokens.RefreshToken -Scopes 'https://outlook.office365.com/.default' -ServicePrincipal -Tenant $CustomerTenantId
        }
        catch {
            Write-Error "Failed to generate a token for Exchange Online: $_"
        }
        try {
            Connect-ExchangeOnline -DelegatedOrganization $CustomerTenantId -AccessToken $exchangeToken.AccessToken -ShowBanner:$false
        }
        catch {
            Write-Error "Failed to connect to Exchange Online: $_"
        }
    }
}