Private/Connect-CGSite.ps1

function Connect-CGSite {
    <#
    .SYNOPSIS
        Reconnect to one site URL using the auth method Connect-CopilotGovernance set up.

    .DESCRIPTION
        During an -AllSites scan the toolkit reconnects to each site in turn. This
        helper reuses whatever authentication Connect-CopilotGovernance established
        (stored in $script:CGAuth): an app-only certificate connection reads every
        site, while a delegated connection is bounded by the signed-in user.

        If no context has been stored yet (someone called a scan cmdlet with -ClientId
        and no prior Connect-CopilotGovernance), it falls back to an interactive connect
        with that client id, which is the original delegated behaviour.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$Url,
        [string]$ClientId
    )

    $a = $script:CGAuth

    if ($a -and $a.AppOnly) {
        $p = @{ Url = $Url; ClientId = $a.ClientId; Tenant = $a.Tenant }
        if ($a.Thumbprint) { $p.Thumbprint = $a.Thumbprint }
        else {
            $p.CertificatePath = $a.CertificatePath
            if ($a.CertificatePassword) { $p.CertificatePassword = $a.CertificatePassword }
        }
        Connect-PnPOnline @p -ErrorAction Stop -Verbose:$false
        return
    }

    $cid = if ($a -and $a.ClientId) { $a.ClientId } elseif ($ClientId) { $ClientId } else { $null }
    if (-not $cid) {
        throw "No client id available. Run Connect-CopilotGovernance first, or pass -ClientId."
    }
    Connect-PnPOnline -Url $Url -Interactive -ClientId $cid -ErrorAction Stop -Verbose:$false
}