Public/New-AzureServicePrincipal.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
function New-AzureServicePrincipal {
    param (
        [Parameter(Mandatory=$false,Position=1)]
        $Name
    )

    if (-not (Test-AzureLogin)) {
        throw "Please login to azure 'az login' and select a subscription"
    }

    #
    if (Test-AzureServicePrincipal) {

        if (-not $ENV:TF_VAR_azure_client_secret){
            throw "Service Principal [$Name] already exists, please choose another name than [$Name] or set the secret of the existing principal in the environment [set-item -path env:TF_VAR_azure_client_secret -value $Secret]"
        }

        $ServicePrincipal = Invoke-Azcli -ArgumentList 'ad','sp','list',"--display-name $Name"

    } else  {
        $ServicePrincipal = Invoke-Azcli -ArgumentList 'ad','sp','create-for-rbac',"-n $Name"
    }

    Write-Output $ServicePrincipal

}