Private/Test-IsRmConnected.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 AzureRm.Profile v3.0 or later is already connected to Azure
.DESCRIPTION
    Test if the AzureRm.Profile v3.0 or later is already connected to Azure
#>

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

    if (-not (Get-Module AzureRm.Profile)) {
        Import-Module AzureRm.Profile
    }
    
    [Boolean] $toReturn = $false
    $azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
    if (-not $azureRmProfile.Accounts.Count) {
        $toReturn = $false
    } else {
        $toReturn = $true
    }
    Write-Debug '-- end - Test-IsRmConnected --'
    return $toReturn

}