Take a look at the Join-Object script from the Powershell team.
Powershell to marge two output
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
Windows for business Windows Server User experience PowerShell
2 answers
Sort by: Most helpful
-
-
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.ps1function 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