azure - ARM模板: how to read output result in array format

标签 azure powershell azure-resource-manager azure-rm-template

我在 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

enter image description here

我需要从返回的数组中提取值。在 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/

相关文章:

ios - Swift 3 Azure Blob 存储数据(图像、视频)使用 SAS 上传

powershell - 如何获得空列表而不是空列表?

azure - 是否可以通过 ARM 模板将 API 导入 Azure API 管理?

powershell - 将 WMI 调用转换为 CIM 调用

azure - 如何在ARM模板中设置变量的值? Or 在 ARM 模板中使用 Or 条件

azure - 我可以在 Azure 模板规范的 uiFormDefinition 中使用引用函数和 UI 控件(例如 'Microsoft.Solutions.ArmApiControl')吗?

用于集成测试的 Azure AD 承载 token ?

azure - QNAmaker QNA 对的最大数量

azure - 将 Azure SAP 或 SharePoint 连接器连接到 OnPremise 失败

string - Powershell中字符串内变量的替代成员