Public/Acronis/Save-AcronisO365Session.ps1

function Save-AcronisO365Session {
    <#
    .SYNOPSIS
        Seeds (or refreshes) the jlhosting.dk AAD session cookie used for fully
        automated Acronis M365 onboarding. Opens a Chrome/Edge window for the
        supporter to sign in, captures the session cookies, and stores them
        encrypted in the module token cache.
 
    .DESCRIPTION
        The captured cookie is valid for $script:TokenCacheConfig.CookieTtlDays
        days (default 90). Re-run this when Start-AcronisAutomatedSetup reports
        that the session cookie is missing or expired. Use -ForceRefresh to
        overwrite an existing valid cookie.
 
    .PARAMETER TenantId
        Optional. If provided, signs in at this specific tenant authority instead
        of the default 'common' (partner home tenant). Normally not needed.
 
    .PARAMETER ForceRefresh
        Overwrite an existing valid session cookie instead of skipping.
 
    .PARAMETER CdpPort
        Chrome DevTools Protocol port. Default 9222.
 
    .EXAMPLE
        Save-AcronisO365Session
 
    .EXAMPLE
        Save-AcronisO365Session -ForceRefresh
    #>

    [CmdletBinding()]
    param(
        [Parameter()]
        [string]$TenantId,

        [Parameter()]
        [switch]$ForceRefresh,

        [Parameter()]
        [int]$CdpPort = 9222
    )

    process {
        $entry = Save-AcronisO365SessionCookie -TenantId $TenantId -ForceRefresh:$ForceRefresh -CdpPort $CdpPort
        if ($entry) {
            $daysLeft = [Math]::Max(0, [Math]::Floor(($entry.ExpirationDateTime - (Get-Date)).TotalDays))
            Write-ModuleLog -Message "Acronis O365 session cookie saved. Expires $($entry.ExpirationDateTime) ($daysLeft days left)." -Level Info -Component 'AcronisO365Session'
            return $entry
        }
    }
}