public/Utils/Merge-Display.ps1
function Merge-Display{ param( [Parameter(Mandatory=$true)] $object, [Parameter(Mandatory=$true)] $object2, [Parameter(Mandatory=$true)] [string]$groupBy, [string[]]$compareBy ) $retValue = @() $item = [PSCustomObject[]]@{$($groupBy)=''} foreach ($member in ($object[0].PSCustomObject.Members | Where-Object MemberType -like NoteProperty)) { if ($member.Name -ne $groupBy){ $item | Add-Member $member.Name '' $item | Add-Member $($member.Name + '2') '' } } if ($compareBy){ foreach ($i in $compareBy) { $item | Add-Member "compare-$i" '' } } foreach ($i in $object) { $objItem = Copy-Object $item foreach ($member in ($i.PSCustomObject.Members | Where-Object MemberType -like NoteProperty)) { $objItem.$($member.Name) = $member.Value } $retValue = $retValue + $objItem } foreach ($i in $object2) { $isNew = $false $objItem = $retValue | Where-Object { $.$groupBy -eq $i.$groupBy } if ($null -eq $objItem) { $isNew = $true $objItem = Copy-Object $item } foreach ($member in ($i.PSCustomObject.Members | Where-Object MemberType -like NoteProperty)) { if ($member.Name -ne $groupBy){ $objItem.$($member.Name + '2') = $member.Value } else { $objItem.$($member.Name) = $member.Value } } if ($isNew) { $retValue = $retValue + $objItem } if ($compareBy){ foreach ($i in $compareBy) { try { $calc = (100*($objItem.$($i + '2') - $objItem.$i)/($objItem.$i)) if ($calc -eq 0) { $stringCalc = '' } else{ if ($calc -gt 0) { $stringCalc = ("+{0:P1}" -f $calc) } else { $stringCalc = ("{0:P1}" -f $calc) } } $objItem."compare-$i" = $stringCalc } catch { $objItem."compare-$i" = '' } } } } return ($retValue | Sort-Object $groupBy) } |