2014-06-27 81 views
2

我在以下測試腳本中遇到問題。 我期待以下結果作爲它的輸出。Powershell:無法將成員添加到集合

VM Name  vCPU    Memory             
-------   ----    ------           
Server1   1                   
Server2        8 

不幸的是,我收到以下輸出。有人能幫我理解代碼中的錯誤嗎?

enter code here 

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 

回答

0

呢?你需要添加蘋果與蘋果,因爲蘋果與土豆可以給予奇怪的行爲

$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