Export-csv not adding all columns

I’m exporting a PSCustomObject to csv. When printing the array on screen, it shows all properties. When exporting the array to CSV. It only shows Disk1 and none of the other disks:

$FolderName = "test" $objs = @() get-folder $FolderName | Get-VM | % { $vmdetails = $_ | get-view $objNew = [PSCustomObject]@{ Name = $_.Name ConfigVersion = $vmdetails.Config.Version GuestToolsVersion = $vmdetails.guest.ToolsVersion NumCpu = $vmdetails.summary.config.numcpu MemoryGB = $_.memorygb IPAddress = $vmdetails.guest.IpAddress Adapter = ($_ | Get-Networkadapter).type VLAN = ($_ | Get-Networkadapter).NetworkName MAC = ($_ | Get-Networkadapter).MacAddress } $disks = $_ | Get-HardDisk $diskCount = 1 foreach ($disk in $disks) { $columnName = "Disk$diskCount" write $columnName $objNew | Add-Member -MemberType NoteProperty -Name $columnName -Value $disk.CapacityGB $diskCount +=1 } $objs += $objNew } $objs $objs | Export-Csv -Path C:tempexport.csv 

Example of $objs printed on screen:

https://imgur.com/a/j1xNH

Example of csv:

Name,”ConfigVersion”,”GuestToolsVersion”,”NumCpu”,”MemoryGB”,”IPAddress”,”Adapter”,”VLAN”,”MAC”,”Disk1″

What I’ve found is that the first object of $objs only has 1 disk, so I think that’s the reason the other disks aren’t included in the export.

Any idea how to make this work?

submitted by /u/Proxert
[link] [comments]

Leave a Reply