无法计算语言表达式 属性“0”,属性 名称必须是字符串 - 添加 Key Vault 访问策略时出现 ARM 模板错误

The language expression property '0' can't be evaluated, property name must be a string - ARM Template error while adding Key Vault access policy

我一直在处理一个问题,但似乎被卡住了,所以继续询问,以防有人能提供帮助。

为了描述这个问题,我有一个现有的 Azure Key Vault 设置,并希望向该资源组添加一些访问策略。它需要有条件,就好像函数名称是“false”,那么不应将该函数添加到密钥保管库访问策略中。

可变部分:

 "variables": {
    "functionAccess": {
      "value": [
        {
          "name": "[parameters('Function_1')]"
        },
        {
          "name": "[parameters('Function_2')]"
        },
        {
          "name": "[parameters('Function_3')]"
        }
      ]
    }
  }

我的模板:

{
  "apiVersion": "2016-10-01",
  "condition": "[not(equals(variables('functionAccess')[CopyIndex()].name, 'false'))]",
  "copy": {
    "batchSize": 1,
    "count": "[length(variables('functionAccess'))]",
    "mode": "Serial",
    "name": "accessPolicies"
  },
  "name": "[concat(parameters('KeyVault_Name'), '/add')]",
  "properties": {
    "accessPolicies": [
      {
        "tenantId": "[subscription().tenantId]",
        "objectId": "[if(not(equals(variables('functionAccess')[CopyIndex()].name, 'false')), reference(concat('Microsoft.Web/sites/', variables('functionAccess')[CopyIndex()].name), '2016-08-01', 'Full').identity.principalId, json('null'))]",
        "permissions": {
          "keys": [
            "get",
            "list"
          ],
          "secrets": [
            "get",
            "list"
          ],
          "certificates": [
            "get",
            "list"
          ]
        }
      }
    ]
  },
  "type": "Microsoft.KeyVault/vaults/accessPolicies"
}

当我为 Azure Key Vault 部署我的 ARM 模板时,我收到了这条错误消息:

The language expression property '0' can't be evaluated, property name must be a string.

下面也试过了,还是一样的错误:

{
  "apiVersion": "2018-02-14",
  "name": "[concat(parameters('KeyVault_Name'), '/add')]",
  "properties": {
    "copy": [
      {
        "batchSize": 1,
        "count": "[length(variables('functionAccess'))]",
        "mode": "serial",
        "name": "accessPolicies",
        "input": {
          "condition": "[not(equals(variables('functionAccess')[copyIndex('accessPolicies')].name, 'false'))]",
          "tenantId": "[subscription().tenantId]",
          "objectId": "[if(not(equals(variables('functionAccess')[copyIndex('accessPolicies')].name, 'false')), reference(concat('Microsoft.Web/sites/', variables('functionAccess')[copyIndex('accessPolicies')].name), '2016-08-01', 'Full').identity.principalId, json('null'))]",
          "permissions": {
            "keys": [
              "get",
              "list"
            ],
            "secrets": [
              "get",
              "list"
            ],
            "certificates": [
              "get",
              "list"
            ]
          }
        }
      }
    ]
  },
  "type": "Microsoft.KeyVault/vaults/accessPolicies"
}

有几个选项可用于为 copy 操作过滤数组。我从 PowerShell 脚本部署我的 ARM 模板并使用 PowerShell 设置参数值。当我需要特殊逻辑处理不同环境的不同输入时,我让 PowerShell 处理它。

如果您必须在 ARM 中处理过滤,并且您可以选择输入 CSV 函数列表,那么下面的方法可能会起作用。然后,您可以使用 functionAccessArraycopy 操作中迭代。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {

  },
  "variables": {
    "functionAccessCsv": "Function-0,Function-1,false,Function-4,false,Function-6,Function-7",
    "functionAccessFiltered": "[replace(replace(variables('functionAccessCsv'), 'false', ''), ',,', ',')]",
    "functionAccessArray": "[split(variables('functionAccessFiltered'), ',')]"
  },
  "resources": [
  ],
  "outputs": {
    "functionAccessCsvFiltered": {
      "type": "string",
      "value": "[variables('functionAccessFiltered')]"
    },
    "functionAccessArray": {
      "type": "array",
      "value": "[variables('functionAccessArray')]"
    }
  }
}

结果:

我刚遇到同样的问题。通过使用具有默认值而不是变量的数组参数,我让它工作了。

"parameters": {
    "functionAccess": {
      "type": "array",
      "defaultValue": [
        "value1",
        "value2",
        "value3"
      ]
    }
  }