0

I am not sure why the output is not working correctly. Can someone please advise. Essentially the array works, except for 1 major issue. everytime a new object gets added to the array it replaces all the previous objects in the array with the last one entered.

I get my array of objects, I just want it to add to the array the new data object, not replace all previous entries and add the new data object.

I can not figure out what I am doing wrong.

$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
}
5
  • You are creating $RptInfo once, then you update this single object each time through your foreach loop and repeatedly add it to your array. You need to create a new object for each iteration. Commented Jun 4, 2018 at 23:05
  • I would also add that it seems unnecessary to manually append an array. Just output each object when you create it. Commented Jun 5, 2018 at 1:46
  • I like both options... now I have one more issue. my out put is duplicated. is there a way to properly grab all unique records of the full record set. Something like $RPTArray = $RPTArray -properties * | select -uniq I know that is not quite correct in powershell. But I would like to remove the duplicate records from the array before I export to csv. Commented Jun 5, 2018 at 23:47
  • I found a solution that might work... Working on testing it. $RPTArray = $RPTArray | Select-Object * -Unique any Suggestion? Commented Jun 6, 2018 at 0:09
  • nope that did not work Commented Jun 6, 2018 at 0:57

2 Answers 2

1

The problem is what's happening when you do this:

$RptArray += $RptInfo

You might be thinking of $RptInfo as a value type, and that you are copying a new value to the array.

$RptInfo is actually a reference. Inside the loop you are simply modifying the existing object, and then adding another reference to this single object onto the array.

This might work:

 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
}

Or, if you are fond of piping

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

    $RptInfoCopy.Counted = $_.Count
    $RptInfoCopy.ItemName = $_.Name
    $RptInfoCopy.ItemType = 'Voice Policy'
    $RptInfoCopy
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was going nuts over this. But what you said makes since... i was using the array as a reference not a value as I intended.
0

For me iterating $VoidPolicy_Table and building $RptArray in one step and populating the three properties looks more stringent:

$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      = "" 
    }
}

3 Comments

I like both options... now I have one more issue. my out put is duplicated. is there a way to properly grab all unique records of the full record set.
Something like $RPTArray = $RPTArray -properties * | select -uniq I know that is not quite correct in powershell. But I would like to remove the duplicate records from the array before I export to csv.
You meant $RPTArray = $RPTArray | Sort-Object -uniq

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.