简体   繁体   English

使用Powershell将数组添加到JSON

[英]Adding Array to JSON using Powershell

I'm trying to set a JSON file using powershell, but the vm's keep appearing in seperate lines. 我正在尝试使用powershell设置JSON文件,但是vm始终出现在单独的行中。

This is the code I use: 这是我使用的代码:

$arrayRg = "VmResGrp"
$vms = vmname
$arrayvms = @(Get-AzureRmVM -ResourceGroupName $arrayRg  | ? {$_.Name -like "*$vms*"}) | select Name 

$data = Get-Content -Path "$updatepath\$encryptParam" -raw | ConvertFrom-Json
$data.parameters.recoveryServicesVaultName.value = "rsvname01"
$data.parameters.recoveryServicesVaultBackupPolicyName.value ="defaultPolicy"
$data.parameters.recoveryServicesVaultResourceGroup.value = "rg-rsvgrp"
$data.parameters.VMNames.value = [array]$arrayvms

$data | ConvertTo-Json -Depth 9 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } |set-content -Path  "$updatedpath\$encryptParam"

The output I get is: 我得到的输出是:

"VMNames":  {
     "value":  [
       {
        "Name":  "vmr1ssr1"
       },
       {
           "Name":  "vmr1ssr2"
       },
       {
           "Name":  "vmr1ssr3"
       }
    ]
 }

What I'm trying to get is: 我想要得到的是:

"VMNames":  {
     "value":  [
       {
         "vmr1ssr1"
         "vmr1ssr2"
         "vmr1ssr3"
       }
    ]
 }

I've tried using -expandProperty on line 3 after select Name but this gives me no output on the values at all. 我尝试在select Name后的第3行上使用-expandProperty ,但这根本没有输出值。

Can anyone see where I'm going wrong? 谁能看到我要去哪里错了?

Thanks in advance :) 提前致谢 :)

I'm pretty sure that what you intend your output to be is actually this: 我很确定您想要的输出实际上是这样的:

{
    "VMNames":  {
                    "value":  [
                                  "vmr1ssr1",
                                  "vmr1ssr2",
                                  "vmr1ssr3"
                              ]
                }
}

The difference being the comma after the name for each VM. 区别在于每个VM名称后的逗号。 The only change you have to make to your code is to expand the Name property, rather than just select it. 您必须对代码进行的唯一更改是扩展Name属性,而不是仅仅选择它。 As it stands you have the line: 从目前的情况来看,您可以执行以下操作:

$arrayvms = @(Get-AzureRmVM -ResourceGroupName $arrayRg  | ? {$_.Name -like "*$vms*"}) | select Name 

This leaves you with 3 objects that have 1 property, Name . 剩下的3个对象具有1个属性Name The value of that property is the relevant content here, so rather than just Select Name you want to use the -ExpandProperty parameter, changing that line to this: 该属性的值是此处的相关内容,因此,您不仅要使用-ExpandProperty参数来Select Name ,还要将该行更改为:

$arrayvms = @(Get-AzureRmVM -ResourceGroupName $arrayRg  | ? {$_.Name -like "*$vms*"}) | select -expand Name 

Now your JSON will look like what I have above. 现在,您的JSON将看起来像我上面的样子。

Edit: If -ExpandProperty does not work you can have PowerShell loop through each object and output the property value with a ForEach-Object loop. 编辑:如果-ExpandProperty不起作用,则可以使PowerShell循环遍历每个对象,并使用ForEach-Object循环输出属性值。 I use the shorter alias of % here: 我在这里使用%的较短别名:

$arrayvms = @(Get-AzureRmVM -ResourceGroupName $arrayRg  | ? {$_.Name -like "*$vms*"}) | % Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM