Last active
October 4, 2018 21:56
-
-
Save ljtill/a3658d9aea445372270f1da066590c79 to your computer and use it in GitHub Desktop.
Provides the ability to retrieve an access token from an Azure AD Application
This file contains hidden or 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
| function Get-AzureRmAccessToken { | |
| <# | |
| .SYNOPSIS | |
| Get-AzureRmAccessToken will retrieve an Access Token via Azure Active Directory. | |
| .DESCRIPTION | |
| Provides the ability to retrieve an Access Token from Azure Active Directory Application. | |
| .EXAMPLE | |
| Get-AzureRmAccessToken -TenantId $tenantId -ClientId $clientId -ClientSecret $clientSecret | |
| .LINK | |
| https://gist.github.com/ljtill | |
| #> | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter()] | |
| [string]$TenantId, | |
| [Parameter()] | |
| [string]$ClientId, | |
| [Parameter()] | |
| [string]$ClientSecret | |
| ) | |
| begin { | |
| $request = @{ | |
| Method = "POST" | |
| Uri = ("https://login.microsoftonline.com/" + $tenantId + "/oauth2/token") | |
| Headers = @{ | |
| "Content-Type" = "application/x-www-form-urlencoded" | |
| "Accept" = "application/json" | |
| } | |
| Body = @{ | |
| "grant_type" = "client_credentials" | |
| "client_id" = $clientId | |
| "client_secret" = $clientSecret | |
| "resource" = "https://management.azure.com/" | |
| } | |
| } | |
| } | |
| process { | |
| $response = Invoke-RestMethod @request | |
| } | |
| end { | |
| return $response.access_token | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Enhanced verbose output.