Private/Assert-MgWindowsUpdatesGraphContext.ps1

function Assert-MgWindowsUpdatesGraphContext {
    [CmdletBinding()]
    param(
        [string[]] $RequiredScope,

        [string[]] $AnyScope,

        [AllowNull()]
        [object] $Context
    )

    $context = if ($PSBoundParameters.ContainsKey('Context')) {
        $Context
    }
    else {
        Get-MgContext -ErrorAction Stop
    }
    if (-not $context) {
        throw 'No active Microsoft Graph session. Call Connect-MgGraph before running this command.'
    }

    if ($context.AuthType -ne 'AppOnly' -and $context.Scopes) {
        if ($RequiredScope) {
            $missingScopes = @(
                $RequiredScope |
                    Where-Object { $_ -notin $context.Scopes }
            )
            if ($missingScopes.Count -gt 0) {
                throw (
                    'The active Microsoft Graph session is missing required scope(s): ' +
                    ($missingScopes -join ', ') +
                    '. Reconnect once with the required scopes before running this command.'
                )
            }
        }

        if ($AnyScope) {
            $matchingScopes = @(
                $AnyScope |
                    Where-Object { $_ -in $context.Scopes }
            )
            if ($matchingScopes.Count -eq 0) {
                throw (
                    'The active Microsoft Graph session requires one of these scopes: ' +
                    ($AnyScope -join ', ') +
                    '. Reconnect once with a required scope before running this command.'
                )
            }
        }
    }

    return $context
}