gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:38:23 31 4
gpt4 key购买 nike

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

当我执行 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')

如果您需要四舍五入到最接近的半小时,则日期时间对象的情况会变得更加复杂,因为没有构建四舍五入的方法。

假设您想要排除 future 的时间戳,向下舍入到最后 30 分钟应该没问题:

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

关于json - 请求中的参数 NO_PARAM 无效请在使用 Powershell 部署 azure arm 模板时为参数 NO_PARAM 提供正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60596048/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com