无法使用 powershell 脚本中的 az cli 使用参数部署 Azure ARM
Unable to deploy Azure ARM with parameters using az cli from powershell script
尝试使用来自 PS 脚本的参数进行简单部署:
$prefix = "xxx"
$location = "switzerlandnorth"
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode incremental `
--verbose `
--template-file .\src\ops\scripts\arm.json `
--parameters "{ `"location`": { `"value`": `"$location`" },`"projectPrefix`": { `"value`": `"$prefix`" } }"
响应错误:
Unable to parse parameter: { location: { value: switzerlandnorth }, projectPrefix: { value: xxx } }
运行 来自 PS1 脚本.
正如我们在错误中看到的,它无法解析参数。给az deployment group create
命令传递参数的正确方法是:
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode "incremental" `
--verbose `
--template-file ".\src\ops\scripts\arm.json" `
--parameters '{ \"location\": { \"value\": \"switzerlandnorth\" },\"projectPrefix\": { \"value\": \"xxx\" } }'
更新:
如果您想在参数中传递 PowerShell 变量,您可以执行如下操作 -
$location = "switzerlandnorth"
$projectPrefix = "xxx"
$params = '{ \"location\": { \"value\": \" ' + $location + '\" },\"projectPrefix\": { \"value\": \"' + $projectprefix + '\" } }'
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode "incremental" `
--verbose `
--template-file ".\src\ops\scripts\arm.json" `
--parameters $params
希望对您有所帮助!
您可以想到的另一种方式是在 PS 中使用 objects/hashtables,然后转换为 JSON,例如
$param = @{
location = "switzerlandnorth"
prefix = "foo"
}
$paramJSON = ($param | ConvertTo-Json -Depth 30 -Compress).Replace('"', '\"') # escape quotes in order to pass the command via pwsh.exe
az deployment group create -g $resourceGroupName--template-file "azuredeploy.json" -p $paramJson
这是一个类似的例子:
https://gist.github.com/bmoore-msft/d578c815f319af7eb20d9d97df9bf657
尝试使用来自 PS 脚本的参数进行简单部署:
$prefix = "xxx"
$location = "switzerlandnorth"
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode incremental `
--verbose `
--template-file .\src\ops\scripts\arm.json `
--parameters "{ `"location`": { `"value`": `"$location`" },`"projectPrefix`": { `"value`": `"$prefix`" } }"
响应错误:
Unable to parse parameter: { location: { value: switzerlandnorth }, projectPrefix: { value: xxx } }
运行 来自 PS1 脚本.
正如我们在错误中看到的,它无法解析参数。给az deployment group create
命令传递参数的正确方法是:
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode "incremental" `
--verbose `
--template-file ".\src\ops\scripts\arm.json" `
--parameters '{ \"location\": { \"value\": \"switzerlandnorth\" },\"projectPrefix\": { \"value\": \"xxx\" } }'
更新:
如果您想在参数中传递 PowerShell 变量,您可以执行如下操作 -
$location = "switzerlandnorth"
$projectPrefix = "xxx"
$params = '{ \"location\": { \"value\": \" ' + $location + '\" },\"projectPrefix\": { \"value\": \"' + $projectprefix + '\" } }'
az deployment group create `
--name $timestamp `
--resource-group $resourceGroupName `
--mode "incremental" `
--verbose `
--template-file ".\src\ops\scripts\arm.json" `
--parameters $params
希望对您有所帮助!
您可以想到的另一种方式是在 PS 中使用 objects/hashtables,然后转换为 JSON,例如
$param = @{
location = "switzerlandnorth"
prefix = "foo"
}
$paramJSON = ($param | ConvertTo-Json -Depth 30 -Compress).Replace('"', '\"') # escape quotes in order to pass the command via pwsh.exe
az deployment group create -g $resourceGroupName--template-file "azuredeploy.json" -p $paramJson
这是一个类似的例子: https://gist.github.com/bmoore-msft/d578c815f319af7eb20d9d97df9bf657