Public/Set-AzureAccountInfo.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
function Set-AzureAccountInfo {

    param (
        [Parameter(Mandatory=$true,Position=1)]
        [string]$Prefix
    )

    $Account = Invoke-Azcli -ArgumentList 'account','show'
    Write-Verbose "Account is: [$Account]"
    $ADUser = Invoke-Azcli -ArgumentList 'ad','user','show',"--upn-or-object-id $($Account.user.name)"
    Write-Verbose "User is: [$ADUser]"

    $VarName = "$($Prefix)user_id"
    Write-Verbose "Setting environment variable [$VarName]"
    Set-Item -Path "Env:$VarName" -Value $($ADUser.objectId) -Force

    $VarName = "$($Prefix)azure_subscription_id"
    Write-Verbose "Setting environment variable [$VarName]"
    Set-Item -Path "Env:$VarName" -Value $($Account.id) -Force

    $VarName = "$($Prefix)azure_tenant_id"
    Write-Verbose "Setting environment variable [$VarName]"
    Set-Item -Path "Env:$VarName" -Value $($Account.tenantId) -Force

}