我在 stackoverflow 上发布了一些问题,感谢那些花时间回答的人。 尽管它存在文档,但我仍然面临与输出功能相关的问题。
基本上我了解如何在格式为字符串时检索数据。 不幸的是,当数据采用数组格式时,对我来说看起来更困难。
"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 中我希望使用类似的东西
- $arrayWebsitesName[0] 获取 AppService-Prod-CentralUS
- $arrayWebsitesName[\1] 获取 AppService-Prod-EastUS
我相信有一些与转换相关的东西,我尝试了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 数组可能不是最有效的,但我通常不处理大型数组,并且性能不是问题(完成工作)。
关于azure - ARM模板: how to read output result in array format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64785046/