使用 ARM 输出或 powershell 获取 Azure Function 默认密钥的方法

krb*_*224 5 powershell azure azure-functions

我正在尝试为 Azure Function 应用设置集成测试。部署进行得很顺利,但我需要一种方法来以编程方式获取默认密钥来运行我的集成测试。

我已经尝试了此处链接的内容 -在 Powershell 中获取 Azure 函数的函数和主机密钥- 但无法在我的 ARM 部署模板中获取 listsecrets。无法识别 Listsecrets。

有谁知道如何使用 ARM 模板和/或 powershell 获取此密钥?

Rob*_*eer 1

更新 Microsoft 的 ARM API 后,现在可以直接从 ARM 部署输出检索 Azure 函数密钥。

例子

{
  "$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')]"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

输出

Outputs 属性将包含一个Newtonsoft.Json.Linq.JObject条目,其中包含 Azure Function 的所有键,即主键、系统键和功能键(包括默认键)。不幸的是,JObject 与部署变量类型的结合有点曲折,需要注意的是,它区分大小写。(如果您在 PowerShell 中工作,则可以将其进行按摩hashtables以供使用。请参阅下面的奖励。)

$results = New-AzResourceGroupDeployment...
$keys = results.Outputs.functionKeys.Value.functionKeys.default.Value
Run Code Online (Sandbox Code Playgroud)

奖金

下面的代码消除了额外的.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 }

}
Run Code Online (Sandbox Code Playgroud)