Skip to content

Instantly share code, notes, and snippets.

@ljtill
Last active October 4, 2018 21:56
Show Gist options
  • Select an option

  • Save ljtill/a3658d9aea445372270f1da066590c79 to your computer and use it in GitHub Desktop.

Select an option

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
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
}
}
@ljtill
Copy link
Author

ljtill commented Jul 4, 2018

TODO: Enhanced verbose output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment