Hello everyone,
I face a problem in following test script.
I expect the following result as the output of it.
VM Name vCPU Memory
------- ---- ------
Server1 1
Server2 8
unfortunately, i am receiving the following output. Can someone help me to understand the mistake in the code?
VM Name vCPU
------- ----
Server1 1
Server2
$obj_collection = @()
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server1"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value "1"
$obj_collection += $report_data
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server2"
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value "8"
$obj_collection += $report_data
$obj_collection
The collection takes the first object you add to it as the layout for the complete collection.
So you have to use all the properties, but can assign them a $null value.
Something like this
$obj_collection = @()
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server1"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value "1"
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value $null
$obj_collection += $report_data
$report_data = New-Object System.Object
$report_data | Add-Member -MemberType NoteProperty -Name "VM Name" -Value "Server2"
$report_data | Add-Member -MemberType NoteProperty -Name "vCPU" -Value $null
$report_data | Add-Member -MemberType NoteProperty -Name "Memory" -Value "8"
$obj_collection += $report_data
$obj_collection
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If you know exactly properties you're interested in:
$obj_collection | ft -Property "VM Name",vCPU,Memory