You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell数组传递给ARM模板覆盖参数时类型不匹配报错求助

解决ARM模板传递数组参数类型不匹配问题

问题场景

我有一个PowerShell脚本,返回包含公网IP名称和地址的对象数组,格式如下:

[{"name":"pip-1","Address":"1.2.3.4"},{"name":"pip2","Address":"5.6.7.8"},{"name":"pip-3","Address":"6.5.4.3"}]

通过以下代码将该数组设置为流水线输出变量,供后续ARM模板使用:

$jsonresult = $ipObj| ConvertTo-Json -Compress

Write-Host $jsonresult 

Write-Output "##vso[task.setvariable variable=FirewallRules;isOutput=true]$jsonresult "

随后在ARM模板部署任务的覆盖参数中传递该变量:

overrideParameters: > -FirewallRules"$(FirewallRules)"

ARM模板中定义的firewallRules参数类型为数组:

"firewallRules": {
      "type": "array",
    },

但流水线执行时报错:

The provided value for the template parameter 'firewallRules' is not valid. Expected a value of type 'Array', but received a value of type 'String'

解决方案

1. 修正PowerShell变量输出的转义逻辑

通过##vso命令传递JSON数组时,需要转义JSON内部的双引号,避免流水线将整个JSON解析为字符串。修改PowerShell脚本如下:

$jsonresult = $ipObj | ConvertTo-Json -Compress
# 转义JSON中的双引号,确保流水线识别为数组结构
$escapedJson = $jsonresult -replace '"', '\"'
Write-Output "##vso[task.setvariable variable=FirewallRules;isOutput=true]$escapedJson"

2. 调整ARM部署任务的参数传递格式

在YAML的overrideParameters中,无需给变量额外添加引号,直接传递转义后的变量即可,同时保证YAML格式正确:

overrideParameters: >
  -firewallRules $(FirewallRules)

如果使用单行格式,写法如下:

overrideParameters: "-firewallRules $(FirewallRules)"

3. 验证变量内容的正确性

可以在ARM部署任务前新增一个PowerShell任务,输出变量的实际内容,确认它是正确的JSON数组格式,没有被多余引号包裹:

Write-Host "FirewallRules变量内容: $(FirewallRules)"

内容的提问来源于stack exchange,提问作者Zucoa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 12:02:52