将数组从 powershell 脚本输出传递到 Arm 模板作为覆盖参数时出错?

问题描述 投票:0回答:1

所以我有一个 powershell 脚本,它返回公共 ip 名称和 publicip 地址的对象类型数组,如下所示:

[{"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)"

然后在模板中我设置了防火墙规则的这个参数:

"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'

我尝试按照建议对其进行编码这里

这个人还说:“如果没有正确转义,用 json 字符串覆盖arm模板参数会给你带来麻烦”

有没有办法以某种方式格式化输出,这样我就不会出现此错误?我需要使用 powershell 脚本的输出动态地填写该参数。

谢谢你。

azure powershell templates arm
1个回答
0
投票

我认为问题是输入数据格式不正确。您可以运行下面的代码并确保 $ipObj 变量将是一个数组,$jsonresult 变量将是一个字符串。

$ipObj = @(
    @{
        "name" = "pip-1"
        "Address" = "1.2.3.4"
    },
    @{
        "name" = "pip2"
        "Address" = "5.6.7.8"
    },
    @{
        "name" = "pip-3"
        "Address" = "6.5.4.3"
    }
)
$jsonresult = $ipObj | ConvertTo-Json

$ipObj.GetType()
$jsonresult.GetType()
© www.soinside.com 2019 - 2024. All rights reserved.