internal/functions/Test-GraphConnection.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function Test-GraphConnection
{
    [CmdletBinding()]
    Param (
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )    
    
    process
    {
        if (Get-MgContext) { return    }

        Write-PSFMessage -Level Error -String 'Test-GraphConnection.Failed' -FunctionName $cmdlet.CommandRuntime
        
        $exception = New-Object System.Data.DataException("No Microsoft Graph connection!")
        $errorID = 'NotConnected'
        $category = [System.Management.Automation.ErrorCategory]::NotSpecified
        $recordObject = New-Object System.Management.Automation.ErrorRecord($exception, $errorID, $category, $Cmdlet)
        $cmdlet.ThrowTerminatingError($recordObject)                
    }
}