Powershell to marge two output

Kalaimani Thirupathi 411 Reputation points
2021-12-22T06:21:06.65+00:00

Dear all,

I need help to merge two output into one result based on the InstanceId.

PS C:\> $backup

Location : Singapore
AccountId : XXXXXXXXX
BackupVaultName : YYYYYYYY
ResourceType : EC2
InstanceId : ZZZZZZZZZZZ
BackupSizeInGB : 100
State : COMPLETED

PS C:\> $vmobjects

Name : AAAAAAAAAA
InstanceState : running
InstanceID : ZZZZZZZZZZZ
InstanceType : c5.2xlarge
LaunchTime : 8/29/2021 7:54:18 PM
BackupPlan : standard

looking for the below result after merging

159500-image.png

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-12-22T13:29:28.88+00:00

    Take a look at the Join-Object script from the Powershell team.

    https://devblogs.microsoft.com/powershell/join-object/

    0 comments No comments

  2. Himanshu Agarwal 1 Reputation point Microsoft Employee
    2021-12-23T10:46:42.013+00:00

    If you paste below code in PS window, it should work if there is no field in common in both objects
    https://www.powershellgallery.com/packages/PSSharedGoods/0.0.31/Content/Public%5CObjects%5CMerge-Objects.ps1

    function Merge-Objects {
    [CmdletBinding()]
    param (
    [Object] $Object1,
    [Object] $Object2
    )
    $Object = [ordered] @{}
    foreach ($Property in $Object1.PSObject.Properties) {
    $Object += @{$Property.Name = $Property.Value}

    }
    foreach ($Property in $Object2.PSObject.Properties) {
    $Object += @{$Property.Name = $Property.Value}
    }
    return [pscustomobject] $Object
    }

    $result = Merge-Objects -Object1 $backup -Object2 $vmobjects

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.