Help needed with PSObject and Export-csv

Hi all.

I have written a short script to load my vm names from a .csv file, and print a line of information on each of the attached disks. I fumbled through getting my info and having it print to the screen, but I’m missing something on using the export-csv function- I suspect it is with the positioning of the statement in my script.

What is happening is that the last server in my list is the only one that is written to the output .csv file at the job’s end. I guess that I don’t have a handle on how to use this PSObject properly. I would appreciate if I could get some guidance on how to properly code the export-csv/ feature. I’m sure that I’m just missing something simple here.


write-host ===========================================
$csv_info = Import-Csv C:\Scripts\jd\jdtool.csv
$timestamp = Get-Date -format “”

foreach ($line in $csv_info) {
$vm = $line.vmname.Trim()
$getvm = Get-VM $vm
$GlobalHDDinfo = Get-HardDisk -VM $vm
$totalHDDs = ($GlobalHDDinfo | group-object | measure-object).count
$report = @()

write-host $vm $totalHDDs
foreach ($vdisk in $GlobalHDDinfo) {
$vDiskSpb = (Get-SpbmEntityConfiguration -HardDisk $vdisk).StoragePolicy
$ctrl = $vdisk.Parent.Extensiondata.Config.Hardware.Device | Where-Object {$_.Key -eq $vdisk.ExtensionData.ControllerKey}
$SCSIid = “$($ctrl.BusNumber):$($vdisk.ExtensionData.UnitNumber)”
$Vmresult = New-Object PSObject

$Vmresult | add-member -MemberType NoteProperty -Name "VMName" -Value $vm
$Vmresult | add-member -MemberType NoteProperty -Name "HDD Name" $vdisk.Name
$Vmresult | add-member -MemberType NoteProperty -Name "VMDK Size (GB)"	$vdisk.CapacityGB
$Vmresult | add-member -MemberType NoteProperty -Name "SCSI ID" $SCSIid
$Vmresult | add-member -MemberType NoteProperty -Name "StoragePolicy" $vDiskSpb
$Vmresult | add-member -MemberType NoteProperty -Name "HDD UUID" $vdisk.ExtensionData.Backing.Uuid

 $report += $vmresult

$report | Export-Csv ./tests_diskinfo_out_$timestamp.csv -NoTypeInformation -UseCulture -Append

Welcome to the forum. :wave:t4:

Could you please make sure ALL your code is formatted as code?

When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

I’d recommend to use [PSCustomObject] instead of the old syntax with New-Object PSObject and Add-Member. It’s way easier to write and a lot easier to read.

That should be all you need:

$csv_info = Import-Csv -Path 'C:\Scripts\jd\jdtool.csv'
$timestamp = Get-Date -Format ''

$report =
foreach ($line in $csv_info) {
    $GlobalHDDinfo = Get-HardDisk -VM $($line.vmname.Trim())

    foreach ($vdisk in $GlobalHDDinfo) {
        $ctrl = 
            $vdisk.Parent.Extensiondata.Config.Hardware.Device | 
                Where-Object { $_.Key -eq $vdisk.ExtensionData.ControllerKey }

            VMName        = $($line.vmname.Trim())
            HDDName       = $vdisk.Name
            VMDK_Size_GB  = $vdisk.CapacityGB
            SCSI_ID       = "$($ctrl.BusNumber):$($vdisk.ExtensionData.UnitNumber)"
            StoragePolicy = (Get-SpbmEntityConfiguration -HardDisk $vdisk).StoragePolicy
            HDD_UUID      = $vdisk.ExtensionData.Backing.Uuid

$report | 
    Export-Csv -Path "./tests_diskinfo_out_$timestamp.csv" -NoTypeInformation -UseCulture
1 Like

Thanks so much Olaf. Sorry about my formatting… will do better for future ones.

I love how much simpler you’ve coded this. Much nicer to use… and more importantly … it works!

I’ll put a few tweaks into it, and then create a similar one for pulling off Network Adapter info.

Have a great day!