请求中的参数 NO_PARAM 无效 请在使用 Powershell 部署 azure arm 模板时为参数 NO_PARAM 提供正确的值

Parameter NO_PARAM in request is invalid Please provide correct value for parameter NO_PARAM while deploying azure arm template using Powershell

我正在尝试使用 powershell 和 arm 模板创建 azure 恢复服务备份保管库。 对于此部署,我创建了部署模板和参数模板。 我正在通过 ConvertFrom-Json 使用 powershell 编辑参数模板 json 文件,然后再次转换回 json 并使用 ConvertTo-Json 保存到模板文件。

当我执行 New-AzResourceGroupDeployment 时,出现如下奇怪的错误 请求中的参数NO_PARAM无效请为参数NO_PARAM

提供正确的值

下面是backuppolicy.deploy.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vaultName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Recovery Services Vault"
      }
    },
    "policyName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Backup Policy"
      }
    },
    "scheduleRunDays": {
      "type": "array",
      "metadata": {
        "description": "Backup Schedule will run on array of Days like, Monday, Tuesday etc. Applies in Weekly Backup Type only."
      }
    },
    "scheduleRunTimes": {
      "type": "array",
      "metadata": {
        "description": "Times in day when backup should be triggered. e.g. 01:00, 13:00. This will be used in LTR too for daily, weekly, monthly and yearly backup."
      }
    },
    "timeZone": {
      "type": "string",
      "metadata": {
        "description": "Any Valid timezone, for example:UTC, Pacific Standard Time. Refer: https://msdn.microsoft.com/en-us/library/gg154758.aspx"
      }
    },
    "weeklyRetentionDurationCount": {
      "type": "int",
      "metadata": {
        "description": "Number of weeks you want to retain the backup"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "apiVersion": "2016-06-01",
      "name": "[concat(parameters('vaultName'), '/', parameters('policyName'))]",
      "type": "Microsoft.RecoveryServices/vaults/backupPolicies",
      "location": "[parameters('location')]",
      "properties": {
        "backupManagementType": "AzureIaasVM",
        "instantRpRetentionRangeInDays": 5,
        "schedulePolicy": {
          "scheduleRunFrequency": "Weekly",
          "scheduleRunDays": "[parameters('scheduleRunDays')]",
          "scheduleRunTimes": "[parameters('scheduleRunTimes')]",
          "schedulePolicyType": "SimpleSchedulePolicy"
        },
        "retentionPolicy": {
          "dailySchedule": null,
          "weeklySchedule": {
            "daysOfTheWeek": "[parameters('scheduleRunDays')]",
            "retentionTimes": "[parameters('scheduleRunTimes')]",
            "retentionDuration": {
              "count": "[parameters('weeklyRetentionDurationCount')]",
              "durationType": "Weeks"
            }
          },
          "retentionPolicyType": "LongTermRetentionPolicy"
        },
        "timeZone": "[parameters('timeZone')]"
      }
    }
  ]
}

下面是backuppolicy.parameters.json

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vaultName": {
      "value": "diskencrypt"
    },
    "policyName": {
      "value": "autoamted-policy-changed"
    },
    "scheduleRunDays": {
      "value": [
        "Monday",
        "Tuesday",
        "Friday"
      ]
    },
    "scheduleRunTimes": {
      "value": [
        "20200309T110019Z"
      ]
    },
    "timeZone": {
      "value": "UTC"
    },
    "weeklyRetentionDurationCount": {
      "value": 5
    }
  }
}

下面是我用来修改json模板和触发部署的powershell代码

$days = "Monday,Tuesday,Friday"
$ScheduleBackupDays = @()
$days = $days.Split(',')
$CurrentTime = @(Get-Date -Format yyyyMMddTHHmmssZ)
foreach($day in $days){
    $ScheduleBackupDays += $day
}
$params = @{
    policyName = "autoamted-policy-changed"
    vaultName = "diskencrypt"
    scheduleRunDays = $ScheduleBackupDays
    scheduleRunTimes = $CurrentTime
    timeZone = "UTC"
    weeklyRetentionDurationCount = 5

}
$templateFile = "$PSScriptRoot\backuppolicy.deploy.json"
$paramtersfile = "$PSScriptRoot\backuppolicy.parameters.json"

$ParameterFileObject = Get-Content -Path $paramtersfile -Raw | ConvertFrom-Json

foreach($Key in $params.Keys){
    $ParameterFileObject.parameters.$Key.value = $params.Get_Item($Key)
}

$ParameterFileObject | ConvertTo-Json -Depth 100 | foreach { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Set-Content -Path $paramtersfile

New-AzResourceGroupDeployment -ResourceGroupName diskencrypt -TemplateFile $templateFile -TemplateParameterFile $paramtersfile

这个例外有点误导。真正的问题是,ARM 无法理解参数文件中的时间戳:

"scheduleRunTimes": {
            "value": [
                "20200309T110019Z"
            ]
        }

这里有两个问题:

  1. 格式错误。 ARM 需要符合 ISO-8601 的 UTC 时间戳,其中包含所有破折号和冒号:2020-03-09T11:00:19Z
  2. 您不能在 scheduleRunTimes 中指定秒数。实际上只允许 30 分钟的步数。

从这个例子的结尾去掉 19 秒,你应该是好的:

"scheduleRunTimes": {
            "value": [
                "2020-03-09T11:00:00Z"
            ]
        }

如果您只需要当前时间,您可以像这样在 powershell 脚本中更改时间戳的格式:

$CurrentTime = @(Get-Date -Format 'yyyy-MM-dd-THH:00:00Z')

如果您需要舍入到最接近的半 n 小时,日期时间对象的情况会变得有点复杂,因为没有内置的舍入方式。

假设您要排除未来的时间戳,四舍五入到最后 30 分钟应该没问题:

$date = Get-Date
$roundedDate = $date.AddMinutes(-($date.Minute % 30))
$CurrentTime = Get-Date $roundedDate -Format 'yyyy-MM-ddTHH:mm:00Z'

您可以在 Parameters.json 文件中设置值 scheduleRunTimes,如下所示 format.ARM 需要符合 ISO-8601 的 UTC 时间戳,所有破折号和冒号:2020-06-26T05:30:00Z

"scheduleRunTimes": {
        "value": [
            "2020-06-26T05:30:00Z"
        ]
    },
    "dailyRetentionDurationCount": {
        "value": 104
    },
    "scheduleRunDays": {
        "value": [
            "Monday",
            "Tuesday",
            "Friday"
        ]
    },

我希望它能奏效。