Public/Get-FatAzConnection.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 Get-AzConnection{
  [CmdLetBinding()]
    param ([string]$subscriptionName)
if ($null -eq (Get-AzContext)){

    Select-AzSubscription -SubScriptionName $subscriptionName  | out-null
    $context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
    
    $graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken
    $aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
    
    if ($null -ne $aadToken){
      Connect-AzAccount -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id
    }
    else{
    Write-Host "No Azure Context... logging in. Check for login window behind this one, especially if you are using VSCode!"
    Connect-AzAccount

    }
  }
Write-Host "Ensuring connected to correct subscription"
if ((Get-AzContext).Subscription.Name -ne $subscriptionName){
  Set-AzContext -SubscriptionName $subscriptionName #-Tenant $tenant
}
}