Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roberto-mardeni/b9ef7384475ad011cc36185bad4a7ce0 to your computer and use it in GitHub Desktop.
Save roberto-mardeni/b9ef7384475ad011cc36185bad4a7ce0 to your computer and use it in GitHub Desktop.
Demonstrates how to execute an Azure Function that has Azure Active Directory Authentication enabled.
$ClientId = "== INSERT CLIENT ID =="
$ClientSecret = "== INSERT CLIENT SECRET =="
$TenantId = "== INSERT TENANT ID =="
$AppId = "== INSERT APP ID =="
$functionUri = "https://myfunctionXYZ.azurewebsites.net"
$functionName = "HttpTrigger1"
$authBody = @{
"tenant" = $TenantId
"client_id" = $ClientId
"scope" = "api://$AppId/.default"
"grant_type" = "client_credentials"
"client_secret" = $ClientSecret
}
$authUrl = "https://login.microsoftonline.com/$tenantId"
$aadtoken = Invoke-RestMethod -Method POST -Uri "$authUrl/oauth2/v2.0/token" -Body $authBody -ContentType "application/x-www-form-urlencoded"
$apiAuthUrl = "$functionUri/.auth/login/aad"
$authBody2 = @{
"access_token" = $aadtoken.access_token
}
$functionToken = Invoke-RestMethod -Method POST -Uri $apiAuthUrl -Body (ConvertTo-Json $authBody2) -ContentType "application/json"
$header = @{
"X-ZUMO-AUTH" = $functionToken.authenticationToken
}
$payload = @{
"name" = "Azure DevOps Pipeline, going to production"
}
Invoke-RestMethod -Method POST -Uri "$functionUri/api/$functionName" -Headers $header -Body (ConvertTo-Json $payload) -ContentType "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment