Working with variables from a JSON

Hello all.
I am banging my head on a issue that I need to figure out.
Basically I have written s PS script that creates a VMware virtual machine in our vcenter based on the variables it gets from a JSON file.

One of the functions I am trying to write into the script is for adding additional HDD’s to the VM.
So in the JSON I have a line for AddDisk:“#”.
Then for each additional disk that is requested there is an entry in the JSON of AdditionalHDSize#:“SizeOfDiskInGB” where the # incraments based on how many additional HDD’s are being requested.

I am trying to write a DO WHILE that will look at the AddDisk number and loop the script to add disks with the Disk size for each additional disk.

The part I am having a hard time with is dynamically updating the variable AdditionalHDSize#

Here is a sample of what I have currently that is not working.

$a = 1

DO
{
$a
$a++

$hdcap = $JsonObject.VMAtt.AdditionalHDSize + "$a"

$vm = Get-VM $vmname

$vm | New-HardDisk -CapacityGB $hdcap -Persistence persistent -Confirm:$false
}
while ($a -le $addDisk)

Thanks in advance!!!

Nathan,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You may share your sanitized JSON file as well (formatted as code please). So we can use it to play around with. :wink:

That sounds unnecessarily complex. Why not simply use an array and iterate over it? :wink:

Thanks. I always forget how to format the code in the post :slight_smile:
The reason for the JSON is we are trying to automate this process with ServiceNow and getting SN to spit out a JSON to a folder that I can monitor looks fairly easy.
Here is the JSON that I am working with.

{
"VMAtt":[{
DataCenter:"NOR",
VMName:"VMNAME",
VMTemplate:"WinSvr2022TMPL",
CPUCount:"2",
RAMCount:"8",
DiskSize:"100",
AddDisk:"4",
UseStaticIP:"Yes",
IPAddress:"x.x.x.x",
Subnet:"255.255.255.0",
Gateway:"x.x.x.x",
DNS1:"x.x.x.x",
DNS2:"x.x.x.x",
AdditionalHDSize2:"100",
AdditionalHDSize3:"200",
AdditionalHDSize4:"300",
AdditionalHDSize5:"400",
TAG1:"TAG1",
TAG2:"TAG",
TAG3:"TAG",
TAG4:"TAG",
TAG5:"TAG",
VMNotes:"Some Notes here"
}
]
}

Thanks a bunch!!!

Your JSON data are actually invalid. Property names have to be enclosed in double quotes. Did you create them by hand or is it from your ServiceNow process?

I thought about something like this:

{
    "VMAtt": [
        {
            "DataCenter": "NOR",
            "VMName": "VMNAME",
            "VMTemplate": "WinSvr2022TMPL",
            "CPUCount": 2,
            "RAMCount": 8,
            "DiskSize": 100,
            "AddDisk": 4,
            "UseStaticIP": true,
            "IPAddress": "x.x.x.x",
            "Subnet": "255.255.255.0",
            "Gateway": "x.x.x.x",
            "DNS1": "x.x.x.x",
            "DNS2": "x.x.x.x",
            "AdditionalHDDSize": [ 100, 200, 300, 400 ],
            "TAG1": "TAG1",
            "VMNotes": "Some Notes here"
        }
    ]
}

Now you can import it and iterate over the array like this:

$inputData = 
    Get-Content -Path D:\sample\VM-Config-Sample.json |
        ConvertFrom-Json
$HDDSizeList =
$inputData.VMAtt | 
    Select-Object -ExpandProperty AdditionalHDDSize
foreach ($HDDSize in $HDDSizeList) {
    "Create additional Disk with the size of '$($HDDSize)' TiB"
}

Thanks!

I will give that a shot.

Not sure what happened when I put the JSON in the code markup but my JSON does have the quites :slight_smile: