Private/Test-IOConnection.ps1

function Test-IOConnection {
    [CmdletBinding()]
    param()

    if (-not $script:IOConnection.Connected) {
        $errorRecord = [System.Management.Automation.ErrorRecord]::new(
            [System.InvalidOperationException]::new(
                "Not connected to Microsoft Graph. Run 'Connect-IdentityOps' first."
            ),
            'IO_NotConnected',
            [System.Management.Automation.ErrorCategory]::ConnectionError,
            $null
        )
        throw $errorRecord
    }

    # Verify the Graph SDK context is still alive
    try {
        $ctx = Get-MgContext -ErrorAction Stop
        if (-not $ctx) {
            throw 'No context'
        }
    }
    catch {
        $script:IOConnection.Connected = $false
        $errorRecord = [System.Management.Automation.ErrorRecord]::new(
            [System.InvalidOperationException]::new(
                "Graph session has expired or been revoked. Run 'Connect-IdentityOps' to reconnect."
            ),
            'IO_SessionExpired',
            [System.Management.Automation.ErrorCategory]::AuthenticationError,
            $null
        )
        throw $errorRecord
    }
}