Private/Test-IsAzConnected.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<#
.SYNOPSIS
    Test if the Az.Accounts is already connected to Azure
.DESCRIPTION
    Test if the Az.Accounts is already connected to Azure
#>

function Test-IsAzConnected {
    [CmdLetBinding()]
    [Outputtype("Boolean")]
    $ErrorActionPreference = 'Stop'
    Write-Debug '-- begin - Test-IsAzConnected --'

    if (-not (Get-Module Az.Accounts)) {
        Import-Module Az.Accounts
    }
    
    $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
    [Boolean] $toReturn = $false
    if (-not $azProfile.Accounts.Count) {
        $toReturn = $false
    } else {
        $toReturn = $true
    }

    Write-Debug '-- end - Test-IsAzConnected --'
    return $toReturn
}