Skip to content

Instantly share code, notes, and snippets.

@lrakai
Last active December 6, 2018 18:13
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 lrakai/51d0c36a74a150dbed15f4c0e3bb82d6 to your computer and use it in GitHub Desktop.
Save lrakai/51d0c36a74a150dbed15f4c0e3bb82d6 to your computer and use it in GitHub Desktop.
Get Azure Token in PowerShell
# Azure Authtentication Token
# From https://blogs.technet.microsoft.com/stefan_stranger/2016/10/21/using-the-azure-arm-rest-apin-get-access-token/
#requires -Version 3
#SPN ClientId and Secret
$ClientID = "clientid" #ApplicationID
$ClientSecret = "ClientSecret" #key from Application
$tenantid = "TenantID"
$TokenEndpoint = {https://login.windows.net/{0}/oauth2/token} -f $tenantid
$ARMResource = "https://management.core.windows.net/";
$Body = @{
'resource'= $ARMResource
'client_id' = $ClientID
'grant_type' = 'client_credentials'
'client_secret' = $ClientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $Body
Method = 'Post'
URI = $TokenEndpoint
}
$token = Invoke-RestMethod @params
$token | select access_token, @{L='Expires';E={[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_.expires_on))}} | fl *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment