Public/Connect/Connect-CustomerGraph.ps1

function Connect-CustomerGraph {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$CustomerTenantId,
        
        [Parameter()]
        [ValidateSet('Application', 'Delegated')]
        [string]$FlowType = 'Application',

        [Parameter()]
        [string[]]$Scopes = @('https://graph.microsoft.com/.default'),

        [Parameter()]
        [switch]$Force
    )

    try {
        # Get token with optional cache bypass
        Write-ModuleLog -Message "Getting Graph token for tenant $CustomerTenantId using $FlowType flow" -Level Verbose -Component 'GraphConnection'
        if ($Force) {
            Write-ModuleLog -Message "Force parameter specified - bypassing token cache" -Level Verbose -Component 'GraphConnection'
        }

        $token = Get-PartnerAccessToken `
            -TenantId $CustomerTenantId `
            -Scopes ($Scopes -join ' ') `
            -FlowType $FlowType `
            -Force:$Force

        # Connect to Graph
        Write-ModuleLog -Message "Connecting to Graph API" -Level Verbose -Component 'GraphConnection'
        $secureToken = ConvertTo-SecureString -String $token.access_token -AsPlainText -Force
        Connect-MgGraph -AccessToken $secureToken -NoWelcome
    }
    catch {
        Write-ModuleLog -Message "Failed to connect to Microsoft Graph for tenant $CustomerTenantId" -Level Error -Component 'GraphConnection' -ErrorRecord $_
    }
}