0

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 :)

4
  • JSON formatting is name/value pairs. Your actual output is working as expected. Are you trying to follow proper JSON notation, or do you want your own proprietary-style format instead? Commented Jan 14, 2019 at 18:49
  • @gravitymixes Thanks for your reply, the question where what I'm trying to achieve is incorrectly formatted. For an array, it should be vmnames: { "value [ **Values** ] }, I believe this is standard, uless I;'m mistaken, although that's also very likely. Commented Jan 14, 2019 at 19:03
  • That is not standard JSON formatting. If you genuinely want the output to look like that, you'd have to design a special parser, and it wouldn't be capable of being interpreted by JSON-standard-specific interpreters. This is a nit-pick, but you also don't have any separating specifiers between each VM name either (, after each value). Commented Jan 14, 2019 at 19:11
  • Hi any idea how i can get this array passed into the JSON Commented Jan 14, 2019 at 22:24

1 Answer 1

1

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. The only change you have to make to your code is to expand the Name property, rather than just select it. 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. 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:

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

Now your JSON will look like what I have above.

Edit: If -ExpandProperty does not work you can have PowerShell loop through each object and output the property value with a ForEach-Object loop. I use the shorter alias of % here:

$arrayvms = @(Get-AzureRmVM -ResourceGroupName $arrayRg  | ? {$_.Name -like "*$vms*"}) | % Name
Sign up to request clarification or add additional context in comments.

3 Comments

@themadtechbician thanks for that, I did actually try that, in my original post I mentioned trying -expandProperty however this didn’t work.. I’ll try with just -expand
-expand is just short for -expandproperty, and that should work. If it does not, try piping to |% Name instead of |Select -Expand Name to see if that works better for you.
Perfect, that works great, thanks. If you add that as an answer I'll accept it, thank you for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.