Skip to content

Instantly share code, notes, and snippets.

@tmeckel
Created December 11, 2020 19:24
Show Gist options
  • Save tmeckel/03491eba97a52e0461af562d2168e3bd to your computer and use it in GitHub Desktop.
Save tmeckel/03491eba97a52e0461af562d2168e3bd to your computer and use it in GitHub Desktop.
Create non-interactive login for AzureAD PowerShell based on an existing AzureRM login
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$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
Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment