我不确定为什么输出无法正常工作。有人可以建议。基本上该阵列工作,除了1个主要问题。每次将新对象添加到数组时,它都会替换最后输入的数组中的所有先前对象。
我得到了我的对象数组,我只是希望它将数据添加到数组中,而不是替换所有以前的条目并添加新的数据对象。
我无法弄清楚我做错了什么。
$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
}
答案 0 :(得分:1)
问题是当你这样做时发生的事情:
$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
})
答案 1 :(得分:0)
对我来说,迭代$ VoidPolicy_Table并在一步中构建$ 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'
$RptArray = ForEach ($VP in $VoicePolicy_Table) {
[PSCustomObject]@{
Counted = $VP.Count
ItemName = $VP.Name
ItemType = 'Voice Policy'
LineURI = ""
RegistrarPool = ""
Identity = ""
Office = ""
OfficeName = ""
DisplayName = ""
Location = ""
VoicePolicy = ""
DialPlan = ""
}
}