lendo
lendo

Reputation: 35

Powershell. Combine properties of objects in array

I'm not very good at powershell and I need help. I am trying to combine properties of objects. For two objects, everything is fine. This is looks like.

$obj1 = [PSCustomObject]@{
    'ip' = '10.10.10.11'
    't1' = 'show'
    't2' = 'ab'

}
$obj2 = [PSCustomObject]@{
    'ip' = '10.10.10.11'
    't1' = 'me'
    't2' = 'cd'
} 

foreach ($prop in $obj2 | Get-Member -MemberType NoteProperty | Where-Object {$_.name -ne 'ip'} | select -ExpandProperty name)
{
    $obj1.$prop += $obj2.$prop
}

$obj1 

Result:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                showme                     abcd  

But I can't do this if many objects in an array. How to do it? Example arrays:

Array1:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                show                       ab                       
10.10.10.12                show                       ab   

Array2:
ip                         t1                         t2                       
--                         --                         --                       
10.10.10.11                me                         cd                       
10.10.10.12                me                         cd  

Upvotes: 2

Views: 1114

Answers (1)

mklement0
mklement0

Reputation: 440142

Basically, all you need is to add an outer loop that iterates over the array elements.

Additionally, you can use the hidden .psobject.Properties member to streamline your solution:

# Sample arrays
$array1 = [pscustomobject]@{ 'ip' = '10.10.10.11'; 't1' = 'show1'; 't2' = 'ab1' },
          [pscustomobject]@{ 'ip' = '10.10.10.12'; 't1' = 'show2'; 't2' = 'ab2' }

$array2 = [pscustomobject]@{ 'ip' = '10.10.10.11'; 't1' = 'me1'; 't2' = 'cd1' },
          [pscustomobject]@{ 'ip' = '10.10.10.12'; 't1' = 'me2'; 't2' = 'cd2' }

# Assuming all objects in the arrays have the same property structure,
# get the list of property names up front.
$mergeProperties = $array1[0].psobject.Properties.Name -ne 'ip'

# Loop over the array elements in pairs and merge their property values.
$i = 0
[array] $mergedArray = 
  foreach ($o1 in $array1) {
    $o2 = $array2[$i++]
    foreach ($p in $mergeProperties) {
        $o1.$p += $o2.$p
    }
    $o1  # output the merged object
  }

Outputting $mergedArray then yields:

ip          t1       t2
--          --       --
10.10.10.11 show1me1 ab1cd1
10.10.10.12 show2me2 ab2cd2

Note:

  • As in your attempt, one of the original objects is modified in place.

  • The assumption is that matching objects in the two arrays are in the same position, respectively (merge the first object from the first array with the first object from the second, ....).

Upvotes: 1

Related Questions