如何使用AZ PowerShell获取Function App的调用URL与密钥?
Absolutely, this is totally achievable—let's break down how to fix your issues and get the correct invocation URL (with custom routes) and function keys in PowerShell.
1. First, Get an Access Token (equivalent to az account get-access-token)
You mentioned not finding a PowerShell equivalent for this, but it does exist! Use Get-AzAccessToken to retrieve a valid token for calling the Azure Management API:
$accessToken = (Get-AzAccessToken -ResourceUrl "https://management.azure.com/").Token
2. Retrieve Function Details (including custom route-compatible invocation template)
The Invoke-AzResourceAction approach wasn't working here because you can't use a function name as an "action" parameter. Instead, directly fetch the function's resource details via the REST API—this will give you the invoke_url_template that respects your custom routes:
# First, get your function app resource (you already have this part) $functionApp = Get-AzWebAppSlot -Name $FunctionAppName -ResourceGroup $ResourceGroupName -Slot $Slot # Build the full resource ID for your specific function $functionResourceId = "{0}/functions/{1}" -f $functionApp.Id, $FunctionName # Use a recent API version (2022-09-01 is more reliable than older versions) $apiUrl = "$functionResourceId?api-version=2022-09-01" # Fetch the function details $functionDetails = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = "Bearer $accessToken"} -Method Get
The $functionDetails.properties.invoke_url_template value will be the base URL template that matches your custom route configuration (e.g., https://yourapp.azurewebsites.net/api/your-custom-route).
3. Get Function Keys (two working methods)
You can use either Invoke-AzResourceAction or the REST API to fetch keys—both work reliably:
Option 1: Using Invoke-AzResourceAction
$functionKeys = Invoke-AzResourceAction -ResourceId $functionResourceId -Action "listkeys" -ApiVersion "2022-09-01" -Force # The default key is stored in $functionKeys.default
Option 2: Using REST API
$keysUrl = "$functionResourceId/listkeys?api-version=2022-09-01" $functionKeys = Invoke-RestMethod -Uri $keysUrl -Headers @{Authorization = "Bearer $accessToken"} -Method Post
4. Build the Final Invocation URL with Custom Route
Now combine the template from step 2 with your function key. If your template already includes your custom route (most cases), just append the key:
# If the template is already your custom route (e.g., https://yourapp.azurewebsites.net/api/HealthCheck) $fullInvocationUrl = "$($functionDetails.properties.invoke_url_template)?code=$($functionKeys.default)"
If you need to replace a placeholder in the template (rare, depending on your route setup), use -replace:
$customRoute = "HealthCheck" $invocationUrl = $functionDetails.properties.invoke_url_template -replace "{route-placeholder}", $customRoute $fullInvocationUrl = "$invocationUrl?code=$($functionKeys.default)"
Why Your Previous Attempts Failed
- The
listsecretsaction returns atrigger_urltied to the default function-name-based route, not your custom route—this is why it didn't work for your setup. - The error
No route registered for '/api/functions/Health?api-version=2019-08-01'happened because you tried to use your function name as an "action" parameter withInvoke-AzResourceAction. Functions don't expose actions named after themselves; you need to fetch the resource directly via GET instead.
内容的提问来源于stack exchange,提问作者David Gard

