Help needed with PSObject and Export-csv

RascalHoudi,
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 'yyyy-MM-dd_HH.mm'

$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 }

        [PSCustomObject]@{
            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