none
How can I Export-CSV a multidimensional array?

    Question

  • I have an array containing a list of VM drive partitions.  The VMs have a different number of partitions.  I am able to collect the data I need, and the array displays all the correct information on the console.  But when I use Export-CSV it will only create the columns that are relevant to the first item it processes; in my case the first VM has 2 partitions.  A column will not be created for any subsequent VMs that have a 3rd or 4th partition, etc.

    $TMPARRAY = new-object system.object

    .......

    Get-HardDisk is from VM PowerCLI, but the context of my questions is related to base PowerShell.

    [array]$VMDKs = Get-HardDisk -vm $VM
    $i=0

    do
    {
    $TMPARRAY | Add-Member -type noteproperty -Name VMDK_$($i) -Value $VMDKs[$i].FileName
    $TMPARRAY | Add-Member -type noteproperty -Name VMDK_$($i)_Size -Value ("{0:N2}" -f ($VMDKs[$i].CapacityKB/1MB) + "GB")
    $i++
    }

    while ($i -lt $VMDKs.count)
    $ARRAY += $TMPARRAY

    $ARRAY contains:

    Guest                : SRV1
    Host                 : HOST1
    Primary_Datastore    : DS1
    Datastore_Size       : 1,023.75GB
    Datastore_Free_Space : 125.42GB
    Guest_Total_Space    : 678.57GB
    VMDK_0               : [DS1] SRV1/SRV1.vmdk
    VMDK_0_Size          : 40.00GB
    VMDK_1               : [DS1] SRV1/SRV1_1.vmdk
    VMDK_1_Size          : 256.00GB

    Guest                : SRV2
    Host                 : HOST2
    Primary_Datastore    : DS3
    Datastore_Size       : 1,023.75GB
    Datastore_Free_Space : 125.42GB
    Guest_Total_Space    : 1,240.46GB
    VMDK_0               : [DS3] SRV2/SRV2.vmdk
    VMDK_0_Size          : 36.00GB
    VMDK_1               : [DS3] SRV2/SRV2_1.vmdk
    VMDK_1_Size          : 256.00GB
    VMDK_2               : [DS3] SRV2/SRV2.vmdk
    VMDK_2_Size          : 618.00GB

    Export-CSV will process SRV1 first and create columns for VMDK_0 and VMDK_1.  For SRV2 and all subsequent VM's, only VMDK_0 and VMDK_1 will be populated, even if values exist for VMDK_2... VMDK_n.  How can I use Export-CSV to have all columns display? 


    Monday, June 18, 2012 3:50 PM

Answers

  • AFAIK, this cannot be done.  Export-Csv bases it's columns on the first record exported.  Export-Csv expects a square array.

    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by tickermcse76 Tuesday, June 19, 2012 11:34 AM
    Monday, June 18, 2012 4:07 PM

All replies

  • AFAIK, this cannot be done.  Export-Csv bases it's columns on the first record exported.  Export-Csv expects a square array.

    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by tickermcse76 Tuesday, June 19, 2012 11:34 AM
    Monday, June 18, 2012 4:07 PM
  • Loop through your $VMDKs collection and add a member for as many disks as you think you may ever need.  then loop through each disk returned and do something like

    $TMPARRAY."VMDK_$($i)"= $VMDKs[$i].FileName
    $TMPARRAY."VMDK_$($i)_Size" = ("{0:N2}" -f ($VMDKs[$i].CapacityKB/1MB) + "GB")

    This would give you a pair of columns for each output, if you want a list of drives you would need to make multiple array elements for each Guest.


    • Edited by Josh Miller 76 Monday, June 18, 2012 8:00 PM removed what BigTeddy had already said.
    Monday, June 18, 2012 7:57 PM
  • Thanks for your responses.  I determined the highest partition count ahead of time, and then for VM's with less than that count I just padded their VMDK_x values wth $NULL.  This is what I came up with:

    $j = get-vm | ? {$_.host -match "mycriteria"} | foreach {($_ | get-harddisk).count} | sort -descending

    [array]$VMDKs = Get-HardDisk -vm $VM
    $i=0

    do
    {
    if ($i -lt $VMDKs.count) {$TMPARRAY | Add-Member -type noteproperty -Name VMDK_$($i) -Value $VMDKs[$i].FileName} else {$TMPARRAY | Add-Member

    -type noteproperty -Name VMDK_$($i) -Value $NULL}

    if ($i -lt $VMDKs.count) {$TMPARRAY | Add-Member -type noteproperty -Name VMDK_$($i)_Size -Value ("{0:N2}" -f ($VMDKs[$i].CapacityKB/1MB) +

    "GB")} else {$TMPARRAY | Add-Member -type noteproperty -Name VMDK_$($i)_Size -Value $NULL}
    $i++
    }

    while ($i -lt $j[0])
    $ARRAY += $TMPARRAY

    Tuesday, June 19, 2012 11:34 AM