Hi Folks,
I have to construct a JSON string containing some basics on my ESXi host and it’s VMs.
$esxihost = 'esxi-01'
Connect-VIServer $esxihost
Get-VMHost -Name $esxihost | Get-View |
Select @{N="host_cpus";E={($_.Hardware.CpuInfo.NumCpuPackages * $_.Hardware.CpuInfo.NumCpuCores)}},
@{N="host_sn";E={(Get-EsxCli).hardware.platform.get().SerialNumber}},
@{N="hostname";E={$_.Name}},
@{N="vms";E={Get-VM |
Select @{N="memory";E={[string]($_.MemoryMB)}},
@{N="name";E={$_.Name }}
}}| ConvertTo-Json -Depth 2
This is the object before it’s converted to JSON
$object | GM
TypeName: Selected.VMware.Vim.HostSystem
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
hostname NoteProperty System.String hostname=ESXI-01.company.local
host_cpus NoteProperty System.Int32 host_cpus=40
host_sn NoteProperty System.String host_sn=8XXXX2
vms NoteProperty System.Object[] vms=
$object | fl
host_cpus : 40
host_sn : 8XXXXX2
hostname : ESXI-01.company.local
vms : {@{memory=16384; name=DC-01}, @{memory=8192; name=test-centos}}
Looks ok, but this is the converted JSON
{
"host_cpus": 40,
"host_sn": "8XXXXX2",
"hostname": "ESXI-01.company.local",
"vms": {
"value": [
{
"memory": "16384",
"name": "DC-01"
},
{
"memory": "8192",
"name": "test-centos"
}
],
"Count": 2
}
}
My problem is the Value and Count properties are being added.
The format I need to send the JSON doesn’t allow these.
{
"host_cpus": 40,
"host_sn": "8XXXXX2",
"hostname": "ESXI-01.company.local",
"vms": {
[
{
"memory": "16384",
"name": "DC-01"
},
{
"memory": "8192",
"name": "test-centos"
}
],
}
}