Export/Private/Test-AzContextHelper.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
28
29
30
function Global:Test-AzContextHelper {
    [CmdletBinding()]
    param()
    process {
        Write-Verbose "Checking for active Azure Connection..."        
        if (-not(Get-AzContext -ErrorAction SilentlyContinue)) {
            Write-Verbose "No active AzContext found. Initiating Connect-AzAccount"
            Connect-AzAccount -Force | Out-Null
        }
        if ($subscriptionName) {            
            $connected = (Get-AzContext).Subscription.Name -eq $subscriptionName
            if (-not($connected)) {    
                Write-Verbose "Selecting correct subscription..."
                if (-not(Get-AzSubscription -SubscriptionName $subscriptionName -ErrorAction SilentlyContinue)) {
                    Write-Verbose "Subscription '$subscriptionName' does not exist in current context. Initiating Connect-AzAccount"
                    Connect-AzAccount -Force | Out-Null
                    if (-not(Get-AzSubscription -SubscriptionName $subscriptionName -ErrorAction SilentlyContinue)) {
                        Write-Verbose "Still couldn't find subscription '$subscriptionName'. Exiting here"
                        throw "Invalid subscription given"                            
                    }
                }
                else {
                    $context = Get-AzSubscription -SubscriptionName $subscriptionName
                    Set-AzContext $context | Out-Null
                }                
            }
        }
        Write-Verbose "Connected. Currently used subscription is: $((Get-AzContext).Subscription.Name)"
    }    
}