Hi,
This might be a bit long but hopefullyl I provide enough info to get an answer :-)
I need to loop over a the results of get-vm and have the results converted to json. Powershell v3 provides convertto-json which works fine on data types it can read, the problems begin when it hits a vmware defined basetype ( i think ).
If I select a few feilds it works.
get-vm | select Name, MemoryMB | convertto-json
[
{
"Name": "testserver1",
"MemoryMB": 6144
},
{
"Name": "testserver2",
"MemoryMB": 512
}
]
If I dont if fails
PS C:\Windows\system32> get-vm | convertto-json
convertto-json : An item with the same key has already been added.
At line:1 char:10
+ get-vm | convertto-json
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertTo-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertToJsonCommand
I believe powershell doesn't know how to loop over some of the vmware defined data types
Using harddisk as an example:-
$data = get-vm
PS C:\Windows\system32> $data[0].HardDisks.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True HardDisk[] System.Array
PS C:\Windows\system32> $data[0].HardDisks | convertto-json
convertto-json : An item with the same key has already been added.
At line:1 char:22
+ $data[0].HardDisks | convertto-json
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertTo-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertToJsonCommand
PS C:\Windows\system32>
PS C:\Windows\system32> $data[0].HardDisks[0] | convertto-json
convertto-json : An item with the same key has already been added.
At line:1 char:25
+ $data[0].HardDisks[0] | convertto-json
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertTo-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertToJsonCommand
PS C:\Windows\system32> $data[0].HardDisks[0].CapacityGB | convertto-json
70
PS C:\Windows\system32> $data[0].HardDisks[0].Name | convertto-json
"Hard disk 1"
It reads the Name as it's an object.
PS C:\Windows\system32> $data[0].HardDisks[0].Name.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
First off can anyone see if there is better way of doing this ? I dont want to hardcode each field and the data type it holds.