VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Export-CSV output shows wrong

Hi,

I am unable to get the output from below script to csv as it shows Length in the output. the screen output shows correctly but unable to get the correct output n csv. Please help

$RDMNAAs = Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select -ExpandProperty ScsiCanonicalName -Unique
$RDMNAAs | ft -auto
$RDMNAAs | Export-Csv -Path ".\RDM_Info.csv" -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is due to the ExpandProperty.
Export-Csv expects objects with a Name and a Value, with ExpandProperty you only provide a Value (a String).

This should work

$RDMNAAs = Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select  -Property ScsiCanonicalName -Unique
$RDMNAAs | Export-Csv -Path ".\RDM_Info.csv" -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

That is due to the ExpandProperty.
Export-Csv expects objects with a Name and a Value, with ExpandProperty you only provide a Value (a String).

This should work

$RDMNAAs = Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select  -Property ScsiCanonicalName -Unique
$RDMNAAs | Export-Csv -Path ".\RDM_Info.csv" -NoTypeInformation -UseCulture


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

0 Kudos
mpeneva
VMware Employee
VMware Employee
Jump to solution

Hi,

Actually the result is correct because the "Export-CSV" method exports the properties of the object. Could you use Out-File method and export the result in csv file format ?

 

Maya

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid that is not correct, the Export-Csv expects an object (Name & Value).
When it only gets a String, it will show the length of that string in the CSV.
Working via Out-File and giving the file a CSV extension will also not work unless you first write a line with the column headers.


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

mpeneva
VMware Employee
VMware Employee
Jump to solution

Thanks, Luc!

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. that worked.

0 Kudos