调用 Azure Powershell New-AzResource 时出错:请求内容无效

Error when calling Azure Powershell New-AzResource: request content invalid

我在调用 New-AzResource 时遇到了这个 Azure PowerShell 错误:

new-azresource : InvalidRequestContent : The request content was invalid and could not be deserialized: 'Could not find member 'dependsOn' on object of type 'ResourceProxyDefinition'. Path 'dependsOn'.'.

我传递给 New-AzResource 的 -属性 参数的对象确实是一个具有 dependsOn 属性 的对象。所以这个消息是一个诱饵。当然,它在 CLI 中工作得很好。

有趣的是,您甚至无法在 Az PS 模块 (https://github.com/Azure/azure-powershell) 的任何代码中找到该消息或子字符串 ResourceProxyDefinition。我已经在我能想到的范围内对此进行了研究,例如查看底层的 Azure API 和源代码。尝试 Google 搜索 ResourceProxyDefinition(无空格),你将主要找到我劫持的 GitHub 问题。

对于 属性 对象,我从 json 模板开始(下面部分给出)。 JSON 直接在 CLI 中工作。但是,对于 azure powershell 模块,我对文件内容使用 ConvertFrom-Json 并将生成的对象作为参数传递。

{
  "type": "Microsoft.VirtualMachineImages/imageTemplates",
  "apiVersion": "2019-05-01-preview",
  "location": "eastus2",
  "dependsOn": [],
  "tags": {
    "imagebuilderTemplate": "My.WindowsServer.2019-Datacenter-1910020354.tpl"
  },

  "properties": {
    "buildTimeoutInMinutes": 100,

    "source": {
      "type": "PlatformImage",
      "publisher": "MicrosoftWindowsServer",
      "offer": "WindowsServer",
      "sku": "2019-Datacenter",
      "version": "2019.0.20190603"
    },

将您的模板更改为如下所示:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
    "name": "My.WindowsServer.2019-Datacenter-1910030425.tpl",
    "type": "Microsoft.VirtualMachineImages/imageTemplates",
    "apiVersion": "2019-05-01-preview",
    "location": "eastus2",
    "dependsOn": [],
    "tags": {
      "imagebuilderTemplate": "My.WindowsServer.2019-Datacenter-1910030425.tpl"
    },

    "properties": {
      "buildTimeoutInMinutes": 100,
      "source": {
        "type": "PlatformImage",
        "publisher": "MicrosoftWindowsServer",
        "offer": "WindowsServer",
        "sku": "2019-Datacenter",
        "version": "2019.0.20190603"

其中包括添加一个额外的级别(将先前的模板嵌套在 "resources" 下并添加一个“$schema”和 "contentVersion" 您已准备好进行部署。

现在,使用:

New-AzResourceGroupDeployment -ResourceGroupName $imageResourceGroup  `
    -TemplateFile $imageTemplateFile

它将读取 JSON 文件并且有效!不要使用 New-AzResource。