通过ARM输出或Powershell获取Azure Function默认密钥的方法 [英] Way to get Azure Function default key with ARM output or powershell
问题描述
我正在尝试为Azure Function应用设置集成测试.部署进行得很好,但我需要一种方法以编程方式获取运行集成测试的默认密钥.
I'm attempting to set up integration testing for an Azure Function app. Deployment is going fine, but I need a way to programatically get the default key to run my integration tests.
我尝试了此处链接的内容-获取功能& Powershell中Azure功能的主机密钥-但无法使listsecret在我的ARM部署模板中工作.无法识别Listsecrets.
I've tried what is linked here - Get Function & Host Keys of Azure Function In Powershell - but cannot get the listsecrets working in my ARM deployment template. Listsecrets is not recognized.
有人知道如何通过ARM模板和/或powershell来获取此密钥吗?
Does anyone know how to get this key with an ARM template and/or powershell?
推荐答案
更新Microsoft的ARM API之后,现在可以直接从ARM部署输出中检索Azure功能键.
After updates to Microsoft's ARM APIs it is now possible to retrieve Azure Function keys directly from the ARM deployment outputs.
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appServiceName": {
"type": "string"
}
},
"variables": {
"appServiceId": "[resourceId('Microsoft.Web/sites', parameters('appServiceName'))]"
},
//... implementation omitted
"outputs": {
"functionKeys": {
"type": "object",
"value": "[listkeys(concat(variables('appServiceId'), '/host/default'), '2018-11-01')]"
}
}
}
输出
Outputs属性将包含一个Newtonsoft.Json.Linq.JObject
条目,其中包含Azure功能的所有键,即主键,系统键和功能键(包括默认键).不幸的是,JObject与部署变量类型结合使用时有点曲折,应该警告您,区分大小写. (如果您在PowerShell中工作,则可以将其按摩到hashtables
中进行消费.请参见下面的奖金.)
Outputs
The Outputs property will contain a Newtonsoft.Json.Linq.JObject
entry that contains all of the keys for the Azure Function i.e., master, system keys, and function keys (which includes the default key). Unfortunately, the JObject combined with the deployment variable type is a bit tortuous to get into and you should be warned, is case sensitive. (If you're working in PowerShell it can be massaged into hashtables
for consumption. See Bonus below.)
$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
奖金
下面的代码摆脱了额外的.Value
调用.
function Convert-OutputsToHashtable {
param (
[ValidateNotNull()]
[object]$Outputs
)
$Outputs.GetEnumerator() | ForEach-Object { $ht = @{} } {
if ($_.Value.Value -is [Newtonsoft.Json.Linq.JObject]) {
$ht[$_.Key] = ConvertFrom-Json $_.Value.Value.ToString() -AsHashtable
} else {
$ht[$_.Key] = $_.Value.Value
}
} { $ht }
}
这篇关于通过ARM输出或Powershell获取Azure Function默认密钥的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!