Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Created March 15, 2019 13:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/69c557f269515ea903364948238a41ab to your computer and use it in GitHub Desktop.
Save brettmillerb/69c557f269515ea903364948238a41ab to your computer and use it in GitHub Desktop.
Get-AzCachedAccessToken
function Get-AzCachedAccessToken {
<#
.SYNOPSIS
Get the Bearer token from the currently set AzContext
.DESCRIPTION
Get the Bearer token from the currently set AzContext. Retrieves from Get-AzContext
.EXAMPLE
Get-AzCachedAccesstoken
.EXAMPLE
Get-AzCachedAccesstoken -Verbose
#>
[cmdletbinding()]
param()
$currentAzureContext = Get-AzContext
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Verbose ("Tenant: {0}" -f $currentAzureContext.Subscription.Name)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId)
$token.AccessToken
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment