Private/Get-365TuneContext.ps1

function Get-365TuneContext {
    <#
    .SYNOPSIS
        Validates Az session and returns active context.
    #>


    $context = Get-AzContext

    if (-not $context) {
        Write-Host " No active Azure session - launching login..." -ForegroundColor Yellow
        Connect-AzAccount -WarningAction SilentlyContinue | Out-Null
        $context = Get-AzContext
    }

    if (-not $context.Tenant.Id) {
        throw "Could not resolve Tenant. Ensure login completed successfully."
    }

    return $context
}