首页 > 解决方案 > Azure Powershell 脚本忽略 defaultValue

问题描述

我正在使用 PowerShell ISE 使用在 Azure 门户中为资源生成的脚本重新创建现有数据库。我发现 PS,不使用脚本路径,所以我在顶部添加了这些行:

Write-Host $PSScriptRoot
Push-Location $PSScriptRoot
Write-Host "Path: $(get-location)"

我添加了一些测试代码以确保 template.json 和 parameters.json 存在并且有效:

# Start the deployment
Write-Host "Starting deployment...";
Write-Host "Testing: $templateFilePath"
if(Test-Path $templateFilePath) {
    Write-Host "FOUND: $templateFilePath"

    if(Test-Path $parametersFilePath) {
        Write-Host "FOUND: $parametersFilePath"
        New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath -TemplateParameterFile $parametersFilePath;
    } else {
        New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath;
    }

}

但是,脚本仍然忽略了 template.json 中的 defaultValue 参数:

"advisors_DropIndex_name": {
    "defaultValue": "DropIndex",
    "type": "String"
},

参数.json:

"advisors_DropIndex_name": {
    "value": null
},

该脚本返回以下内容:

新 AzureRmResourceGroupDeployment:下午 5:17:30 - 错误:代码 = InvalidDeploymentParameterValue;Message=部署参数“ keys_ServiceManaged_name ”的值为空。请指定值或使用参数引用。有关详细信息,请参阅https://aka.ms/arm-deploy/#parameter-file。在 C:\Users\longoj\Downloads\ExportedTemplate-DEEAResourceGroup\deploy.ps1:110 char:9 + New-AzureRmResourceGroupDeployment -ResourceGroupName $resour ... + ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:未指定: (:) [New-AzureRmResourceGroupDeployment],异常 + FullyQualifiedErrorId:Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

如果在 parameters.json 中设置值:

"advisors_DropIndex_name": {
    "value": "DropIndex"
},

脚本验证转到下一个参数,其中有很多。所以看起来 template.json 文件的 defaultvalue 参数被忽略了。

我在用:

Version           : 5.1.2
Name              : Azure
Author            : Microsoft Corporation
PowerShellVersion : 3.0

我究竟做错了什么 ?

标签: powershellazureazure-resource-managerazure-deployment

解决方案


如果在部署期间没有提供值(例如 parameters.json),将使用defaultValue 。

在您的情况下,您提供的是该参数值,该参数值为null,并且每个错误消息都无效(例如 InvalidDeploymentParameterValue)。

如果要应用默认值,请从 parameters.json 文件中删除整个以下代码。

"advisors_DropIndex_name": {
    "value": null
},

在https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-templates-parameters查找更多详细信息。


推荐阅读