To create output in your PowerCLI script like the output of Format-Table, you have to create an output object. There are several ways you can do this. One way is to use the PowerShell New-Object cmdlet as shown in the script below. The properties in the output of the New-Object cmdlet are not ordered. That is why I pipe the output to the Select-Object cmdlet to order them.
Your original script will generate an array for the CapacityGB property, if a VM has more than one hard disk. That is why I introduced a second foreach to loop through the hard disks. Because you probably want to know which CapacityGB belongs to which hard disk I added the 'Hard Disk' property.
The .NET [Math]::Round() function is used to round the CapacityGB to zero decimal places.
The '&' character at the beginning of the script and the scriptblock {} around the script are necessary to make it possible to pipe the ouput of the script to another cmdlet like Export-CSV. foreach is not a cmdlet and therefor can't you use the the output of foreach in the pipeline without using this trick.
& {foreach ($VmItem in $MyVms)
{
foreach ($CurrentItemHdd in (Get-HardDisk -VM $VmItem))
{
New-Object -TypeName PSObject -Property @{
VM = $VmItem.Name
'Hard Disk' = $CurrentItemHdd.Name
CapacityGB = [Math]::Round($CurrentItemHdd.CapacityGB,0)
} |
Select-Object -Property VM,'Hard Disk',CapacityGB
}
}}
Message was edited by: Robert van den Nieuwendijk