Last active
December 6, 2018 18:13
-
-
Save lrakai/51d0c36a74a150dbed15f4c0e3bb82d6 to your computer and use it in GitHub Desktop.
Get Azure Token in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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