ARM模板:如何读取数组格式的输出结果

ARM Template: how to read output result in array format

我在 Whosebug 上发布了一些问题,感谢那些花时间回答的人。 虽然它存在文档,但我仍然面临与输出功能相关的问题。

基本上我了解如何在格式为 string 时检索数据。 不幸的是,当数据采用 array 格式时,对我来说看起来更难。

 "outputs": {
    "keyVaultName": {
        "type": "string",
        "value": "[variables('keyVaultName')]"
    },
    "websitesname": {
        "type": "array",
        "copy": {
            "count": "[length(variables('webSitesName'))]",
            "input": {
                "name": "[variables('webSitesName')[copyIndex()].name]"
            }
        }
    }
}

然后我这样做:

$deploymentResult = New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName 
$ResourceGroupName-TemplateFile $TemplateFile 
$vaultName = $deploymentResult.Outputs.keyVaultName.Value
$arrayWebsitesName = $deploymentResult.Outputs.websitesname.Value

我需要从返回的数组中提取值。在 powershell 中,我希望使用类似

的东西

我相信有一些与转换有关的东西,我尝试了 convertto-json 但没有成功。 感谢您的帮助!

websitesname 的部署返回的变量类型是 JArray

PS> $deploymentResult.Outputs.websitesname.Value.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    JArray                                   Newtonsoft.Json.Linq.JContainer

为了获得这些值,您可以这样调用:

$arrayWebsitesName = $deploymentResult.Outputs.websitesname.Value
$ids = $arrayWebsitesName.ToString() | ConvertFrom-Json
$ids[0].name
$ids[1].name

将 JArray 转换为 JSON 字符串,然后将 JSON 字符串转换为 PowerShell 数组可能不是最有效的,但我通常不处理大型数组,性能也不是一个问题(完成工作)。