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

我已经尝试过这里链接的内容-Get Function & Host Keys of Azure Function In Powershell-但无法在我的ARM部署模板中使用listsecrets。无法识别Listsecrets。

有谁知道如何通过ARM模板和/或Powershell获得此 key ?

最佳答案

更新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')]"
    }
  }
}

产出

Outputs属性将包含一个Newtonsoft.Json.Linq.JObject条目,该条目包含Azure功能的所有键,即主键,系统键和功能键(包括默认键)。不幸的是,JObject与部署变量类型结合使用时有点曲折,应该警告您,区分大小写。 (如果您在PowerShell中工作,则可以将其按摩到hashtables中进行使用。请参见下面的奖励。)
$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 }

}

关于powershell - 通过ARM输出或Powershell获取Azure Function默认 key 的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46736165/

10-13 07:45
查看更多