从 Powershell 获取实际 Azure Function 端点的列表

Get list of actual Azure Function endpoints from Powershell

我正在尝试从 Powershell 脚本获取我的 Azure 函数应用程序中函数端点的完整列表。我可以从 management.azure.com API 中获取函数列表,但它只有函数名称,例如...

/subscriptions/ea4a3766-c3a8-4b9c-xxxx-xxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyFunctionAppName/functions/FunctionName

但该函数实际上有一个端点(例如)http://myfunctionapp.azurewebsites.net/api/allsources/{sourceName}

如何从 Powershell 的 Azure 管理 API 中获取端点名称?它显示在门户的 "Get Function URL" 按钮中,所以我想它一定在某处。

编辑:建议的副本仍然没有提供实际的函数端点。例如,我有一个名为 CheckLock 的函数。根据门户网站上的 "Get Function URL" 按钮(以及我想要的那个),它的端点是:https://myfunctionapp.azurewebsites.net/api/account/lock/{id}?code=myfunctioncode

我从建议的副本中得到的是:

@{
name=CheckLock; 
function_app_id=/subscriptions/ea4a3766-c3a8-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myresourcegroup/providers/Microsoft.Web/sites/myfunctionappname;
script_root_path_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/CheckLock/; 
script_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/bin/Funcs.dll; 
config_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/site/wwwroot/CheckLock/function.json; 
secrets_file_href=https://myfunctionappname.scm.azurewebsites.net/api/vfs/data/functions/secrets/CheckLock.json; 
href=https://myfunctionappname.scm.azurewebsites.net/api/functions/CheckLock;
config=; 
files=; 
test_data=
} 

你可以通过名为 Get Function URLtrigger_url 获得的 url,似乎你无法在一个函数应用程序中获得所有这些,但你可以得到一个具体功能,见:Web Apps - List Function Secrets,响应体中的trigger_url就是你想要的

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/{functionName}/listsecrets?api-version=2016-08-01

响应正文:

 {
   "key": "xxxxxxxxxxxxxxxxxxx",
   "trigger_url": "https://xxxxx.azurewebsites.net/api/HttpTrigger1?code=xxxxxxxxxxxxxxx"
 }

注意:如果出现如下错误,您需要为您的函数应用程序添加一个应用程序设置AzureWebJobsSecretStorageType : Files

{
  "error": {
    "code": "Conflict",
    "message": "System.InvalidOperationException: Runtime keys are stored on blob storage. This API doesn't support this configuration. Please change Environment variable AzureWebJobsSecretStorageType value to 'Files'. For more info, visit https://aka.ms/funcsecrets\r\n   at Kudu.Core.Functions.FunctionManager.<GetKeyObjectFromFile>d__9`1.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Functions\FunctionManager.cs:line 141\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Kudu.Core.Functions.FunctionManager.<GetFunctionSecretsAsync>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Functions\FunctionManager.cs:line 220\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at Kudu.Services.Functions.FunctionController.<GetSecrets>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Services\Functions\FunctionController.cs:line 141"
  }
}

我明白了。在函数本身的数据中,路由是 properties.config 对象的一部分。

请求应如下所示:https://management.azure.com/subscriptions/{subscriptionid}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{functionAppName}/functions/{functionName}?api-version=2016-08-01

在 return 值中是一个 properties 对象,在该对象中是 config 对象。下面是 route 属性,其中包含触发端点。

在 Powershell 中,是这样的:

$functionData = Invoke-RestMethod -Method Get -Uri $functionName -Headers $accessTokenHeader
$triggerUrl = "https://$functionAppName.azurewebsites.net/api/" + $functionData.properties.config.route

你可以在这里测试:https://docs.microsoft.com/en-us/rest/api/appservice/webapps/getfunction

希望这对其他人有帮助!感谢贡献者。

AZ PowerShell 引入了 Invoke-AzResourceAction cmdlet。这完全符合它在罐头上所说的 - 它允许您调用针对 Azure 资源的操作。

可以通过listkeys操作获得功能键和URL如下-

$functionApp = Get-AzWebAppSlot -Name $FunctionAppName -ResourceGroup $ResourceGroupName -Slot $Slot
$functionSecrets = Invoke-AzResourceAction -ResourceId ("{0}/functions/{1}" -f $functionApp.Id, $FunctionName) -Action "listkeys" -ApiVersion "2019-08-01" -Force

变量$functionSecrets现在包含两个属性-

  • key:默认功能级键(仅适用于$FunctionName)。
  • trigger_url:URL触发$FunctionName。请注意,此 属性 不支持自定义路由。

快速提示:如果您可以使用 Get-AzContext 获取 azure 上下文,则可以像这样获取 header 的访问令牌:(Get-AzContext).TokenCache[0].ReadItems().AccessToken