Public/Connect-Az.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
function Connect-Az {
    [OutputType([String])]
    param (
        [ValidateSet('UserInteractive','ClientSecret','ClientCert','SystemAssigned','UserAssigned')]
        [string]$AuthMethod
    )

    $ErrorActionPreference = 'Stop'
    if([string]::IsNullOrEmpty($AuthMethod)) {
        if(![string]::IsNullOrEmpty($env:AzureAuthMethod))
        {
            $AuthMethod = $env:AzureAuthMethod
        }
        else {
            Write-Error "AuthMethod must be set either as an argument or an environment variable."
        }
    }

    $env:AccessAuthMethod = $AuthMethod
    switch ([string]$AuthMethod) {
        'UserInteractive' { AzureLoginUser }
        'ClientSecret' { AzureLoginServicePrincipal }
        'ClientCert' { AzureLoginServicePrincipal -Certificate }
        'SystemAssigned' { AzureLoginManaged }
        'UserAssigned' { AzureLoginManaged -UserAssigned }
    }
}