向数组Powershell添加数据

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (305)

我不知道为什么输出不能正常工作。请有人通知。基本上数组工作,除了一个主要的问题。每次一个新的对象被添加到数组中时,它用最后一个输入替换数组中的所有先前的对象。

我得到了我的对象数组,我只希望它将新的数据对象添加到数组中,而不是替换所有以前的条目并添加新的数据对象。

我不知道我做错了什么。

$RptInfo = New-Object Object -TypeName PSObject
Add-Member -MemberType NoteProperty -Name Counted -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name ItemName -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name ItemType -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name LineURI -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name RegistrarPool -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name Identity -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name Office -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name OfficeName -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name DisplayName -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name Location -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name VoicePolicy -Value "" -InputObject $RptInfo 
Add-Member -MemberType NoteProperty -Name DialPlan -Value "" -InputObject $RptInfo 
[array]$RptArray = @()
$VoicePolicy_Table = Get-CsUser | Where {$_.EnterpriseVoiceEnabled -eq $true} | Sort-Object -Property VoicePolicy | Group-Object -Property VoicePolicy, 'Voice Policy'
$DialPlan_Table = Get-CsUser | Where {$_.EnterpriseVoiceEnabled -eq $true} | Sort-Object -Property DialPlan | Group-Object -Property DialPlan, 'Dial Plan'
foreach ($VP in $VoicePolicy_Table) {
    $RptInfo.Counted = $VP.Count
    $RptInfo.ItemName = $VP.Name
    $RptInfo.ItemType = 'Voice Policy'
    $RptArray += $RptInfo
}
ForEach ($RPT in  $RptArray) {    
    $RPT.ItemName
}
提问于
用户回答回答于

问题是当你这样做的时候会发生什么:

$RptArray += $RptInfo

你可能在想$RptInfo作为价值类型,并且要将新值复制到数组。

$RptInfo实际上是参照系。在循环中,您只需修改现有对象,然后将对该单个对象的引用添加到数组中。

这可能会奏效:

 function New-ReportInfo
{
   $RptInfo = New-Object Object -TypeName PSObject
   Add-Member -MemberType NoteProperty -Name Counted -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name ItemName -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name ItemType -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name LineURI -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name RegistrarPool -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name Identity -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name Office -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name OfficeName -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name DisplayName -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name Location -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name VoicePolicy -Value "" -InputObject $RptInfo 
   Add-Member -MemberType NoteProperty -Name DialPlan -Value "" -InputObject $RptInfo
   $RptInfo
}

[array]$RptArray= @()
foreach ($VP in $VoicePolicy_Table)
{
    $RptInfoCopy = New-ReportInfo

    $RptInfoCopy.Counted = $VP.Count
    $RptInfoCopy.ItemName = $VP.Name
    $RptInfoCopy.ItemType = 'Voice Policy'
    $RptArray += $RptInfoCopy
}

或者,如果你喜欢管道

$RptArray = ($VoidPolicy_Table | % {
    $RptInfoCopy = New-ReportInfo

    $RptInfoCopy.Counted = $_.Count
    $RptInfoCopy.ItemName = $_.Name
    $RptInfoCopy.ItemType = 'Voice Policy'
    $RptInfoCopy
})

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励