VMware Cloud Community
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Nested array with multiple entries?

as a sample code:

$parentarray = @()

$getallvms = get-vm

foreach ($singlevm in $getallvms){

  

     $array1 = "" | Select HardDisk

  

     $harddisk = ($singlevm | Get-harddisk).Filename

     $array1.HardDisk = $Harddisk

  

     $parentarray += $array1

     }

$ParentArray | Export-Csv -Path "C:\blah\whatever.csv"

In the CSV this will return a System.Object[] instead of multiple entries if there are multiple entries.   I'm trying to figure out how to make this command show all the entries under the custom array... any help would be appreciated... Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.

Export-Csv doesn't know how to handle properties that do not hold simple values.

With the -join operator we convert the array of strings into a single string.

$parentarray = @()

$getallvms = Get-VM

foreach ($singlevm in $getallvms){

     $array1 = "" | Select HardDisk

     $harddisk = ($singlevm | Get-harddisk).Filename -join '|'

     $array1.HardDisk = $Harddisk

     $parentarray += $array1

}

$ParentArray | Export-Csv -Path "C:\blah\whatever.csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.

Export-Csv doesn't know how to handle properties that do not hold simple values.

With the -join operator we convert the array of strings into a single string.

$parentarray = @()

$getallvms = Get-VM

foreach ($singlevm in $getallvms){

     $array1 = "" | Select HardDisk

     $harddisk = ($singlevm | Get-harddisk).Filename -join '|'

     $array1.HardDisk = $Harddisk

     $parentarray += $array1

}

$ParentArray | Export-Csv -Path "C:\blah\whatever.csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
CenturaBro
Enthusiast
Enthusiast
Jump to solution

Works good enough for me.

Thanks a bunch.

0 Kudos