使用 AZ PowerShell 获取函数 URL

Get function URL's using AZ PowerShell

在 PowerShell 中,我正在尝试检索函数应用程序的调用 URL 和密钥。经过几个小时的尝试,我在调用 URL 方面遇到了障碍。这真的可能吗?

由于 Microsoft 没有费心提供与 az rest 等效的功能,我一直在查看 Invoke-AzResourceAction,其文档内容如下 -

The Invoke-AzResourceAction cmdlet invokes an action on a specified Azure resource. To get a list of supported actions, use the Azure Resource Explorer tool.

一段时间后使用 Azure Resource Explorer tool I realised that it was not really very helpful, so I started looking for other options and found Get-AzProviderOperation

The Get-AzProviderOperation gets the operations exposed by Azure resource providers.

使用上面的命令我可以看到有三个可用的操作 -

Operation                                        OperationName                   ProviderNamespace  ResourceName       Description            IsDataAction
---------                                        -------------                   -----------------  ------------       -----------            ------------
microsoft.web/sites/functions/action             Functions Web Apps              Microsoft Web Apps Web App            Functions Web Apps.           False
microsoft.web/sites/functions/listsecrets/action List Web Apps Functions Secrets Microsoft Web Apps Web Apps Functions List Function secrets.        False
microsoft.web/sites/functions/listkeys/action    List Web Apps Functions Keys    Microsoft Web Apps Web Apps Functions List Function keys.           False

第一次尝试

最初我很高兴使用 listsecrets 操作,但是在将带有自定义路由的函数添加到我的 Function App 后,我注意到 listsecrets 在 [=18] 中不支持此自定义路由=] 属性,所以我不得不放弃使用它。

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

第二次尝试

接下来我尝试使用 listkeys 操作来获取功能键,并结合 functions 操作。我知道对于 REST API,调用 URL 可以在查询函数时在 properties.invoke_url_template 处找到,所以我希望在这里也一样。唉...

Invoke-AzResourceAction -ResourceId ("{0}/functions" -f $functionApp.Id) -Action $FunctionName -ApiVersion "2019-08-01" -Force

No route registered for '/api/functions/Health?api-version=2019-08-01'

很可能我在这种情况下实施 Invoke-AzResourceAction 不正确(今天之前从未使用过它)。

我想到的其他事情

除了缺少 az rest 的等效项外,AZ PowerShell 还缺少 az-account-get-access-token 的等效项。这意味着我无法获取我的令牌并简单地查询 REST API.

正如我在评论中提到的,如果你想获得函数(触发器)url,包括功能键,你可以使用下面的命令,$FunctionURL 就是你想要的.

$FunctionAppName = "joyfun"
$GroupName = "xxxxx"
$TriggerName = "HttpTrigger1"
$FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName
$Id = $FunctionApp.Id
$DefaultHostName = $FunctionApp.DefaultHostName
$FunctionKey = (Invoke-AzResourceAction -ResourceId "$Id/functions/$TriggerName" -Action listkeys -Force).default
$FunctionURL = "https://" + $DefaultHostName + "/api/" + $TriggerName + "?code=" + $FunctionKey

如果触发器在插槽中,请更改此行 $FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName$FunctionApp = Get-AzWebAppSlot -ResourceGroupName $GroupName -Name $FunctionAppName -Slot <slotname>.

更新:

我测试了我这边的自定义路由,我的 host.json 片段如下所示。

{
  "extensions": {
        "http": {
            "routePrefix": "api/Health",
        }
    }
}

您可以使用下面的命令。

$FunctionAppName = "joyfun"
$GroupName = "xxxxx"
$TriggerName = "HttpTrigger1"
$FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName
$Id = $FunctionApp.Id
$DefaultHostName = $FunctionApp.DefaultHostName
$FunctionKey = (Invoke-AzResourceAction -ResourceId "$Id/functions/$TriggerName" -Action listkeys -Force).default
$invokeurl = (Get-AzResource -ResourceId "$Id/functions" | Where-Object {$_.Name -eq "$FunctionAppName/$TriggerName"}).Properties.invoke_url_template
$FunctionURL = $invokeurl + "?code=" + $FunctionKey