PowerCLI

 View Only
  • 1.  get-vm | convertto-json

    Posted Nov 22, 2012 11:54 AM

    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.



  • 2.  RE: get-vm | convertto-json

    Posted Nov 22, 2012 12:22 PM

    I think the message is clear, there are properties in the object that have the same name.

    And don't forget there is the ExtensionData property that maps the complete underlying vSphere object.

    The following works for

    Get-VM MyVM | Select * -ExcludeProperty ExtensionData | ConvertTo-Json -Depth 1

    Do you want to convert the complete object to Json ?



  • 3.  RE: get-vm | convertto-json

    Posted Nov 22, 2012 12:43 PM

    Thanks for the reply.

    I would like the whole object into json, but once you increase the depth same error

    Get-VM MyVM | Select * -ExcludeProperty ExtensionData | ConvertTo-Json -Depth 2
    ConvertTo-Json : An item with the same key has already been added.
    At line:1 char:73
    + Get-VM "csitestvl01 + adam" | Select * -ExcludeProperty ExtensionData | ConvertT ...
    +                                                                         ~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ConvertTo-Json], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertToJsonCommand

    I think I'm going to have to create my own object and iterate over the get-vm output, which is what I'm trying to avoid.