Adding Array to JSON using Powershell

  • 0
  • Pump00:00

I’m trying to set a JSON file using powershell, but the vm’s keep appearing in seperate lines.

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.

Can anyone see where I’m going wrong?

Thanks in advance 🙂

Default Asked on February 5, 2020 in Programming.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.