Skip to content

Instantly share code, notes, and snippets.

@arsenvlad
Last active September 26, 2017 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsenvlad/2db9e556fe5ddda909ec1ef933cb30a5 to your computer and use it in GitHub Desktop.
Save arsenvlad/2db9e556fe5ddda909ec1ef933cb30a5 to your computer and use it in GitHub Desktop.
# Based on code from https://gallery.technet.microsoft.com/scriptcenter/Easily-obtain-AccessToken-3ba6e593
# Assumes AzureRm.Profile module is improted and is version 3 or newer
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
$currentAzureRmContext = Get-AzureRmContext
$token = $profileClient.AcquireAccessToken($currentAzureRmContext.Subscription.TenantId)
$accessToken = $token.AccessToken
$accessToken
# Invoke Azure Resource Manager REST API
$headers = @{ Authorization = "Bearer $accessToken" }
# List subscriptions
Invoke-WebRequest -uri "https://management.azure.com/subscriptions?api-version=2014-04-01" -Headers $headers -UseBasicParsing | ConvertFrom-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment