Join/merge arrays' properties

Option 1

One option would be to put both object into a HashTable, and then cast it as a PSCustomObject.

 $Obj1 = (Get-Process)[0]; # Get a process
 $Obj2 = (Get-Service)[0]; # Get a service
 ($Result = [PSCustomObject]@{ Obj1 = $Obj1; Obj2 = $Obj2; });

 $Result.Obj1.Name;
 $Result.Obj2.Name;

NOTE: This will create an additional "level" that you have to drill into, so it's not ideal, but it will work.

Option #2

The second option would be to iterate over all the properties of the "second object" and add them to the "first object" using Add-Member, which your example already shows.

Create an empty file called c:\test\test.txt, and then run the following code:

# Get a couple of objects (with different property names)
$Object1 = Get-Service -Name Dnscache;
$Object2 = Get-Item c:\test\test.txt;

# Get a list of the properties on both objects
$PropertyList1 = @(Get-Member -InputObject $Object1 -MemberType Properties).Name;
$PropertyList2 = Get-Member -InputObject $Object2 -MemberType Properties | Where-Object -FilterScript { $PropertyList1 -notcontains $PSItem.Name; };

# Add the properties, from the second object, to the first object
foreach ($Property in $PropertyList2) {
    Write-Host ('Adding property: {0}' -f $Property.Name);
    Add-Member -InputObject $Object1 -Name $Property.Name -MemberType NoteProperty -Value $Object2.$($Property.Name);
}

# Output the object
$Object1 | select *;

The output looks like the following:

Mode                : -a---
PSChildName         : test.txt
PSDrive             : C
PSIsContainer       : False
PSParentPath        : Microsoft.PowerShell.Core\FileSystem::C:\test
PSPath              : Microsoft.PowerShell.Core\FileSystem::C:\test\test.txt
PSProvider          : Microsoft.PowerShell.Core\FileSystem
Attributes          : Archive
CreationTime        : 3/11/2014 3:06:43 PM
CreationTimeUtc     : 3/11/2014 8:06:43 PM
Directory           : C:\test
DirectoryName       : C:\test
Exists              : True
Extension           : .txt
FullName            : C:\test\test.txt
IsReadOnly          : False
LastAccessTime      : 3/11/2014 3:06:43 PM
LastAccessTimeUtc   : 3/11/2014 8:06:43 PM
LastWriteTime       : 3/11/2014 3:06:29 PM
LastWriteTimeUtc    : 3/11/2014 8:06:29 PM
Length              : 0
BaseName            : test
VersionInfo         : File:             C:\test\test.txt
                      InternalName:     
                      OriginalFilename: 
                      FileVersion:      
                      FileDescription:  
                      Product:          
                      ProductVersion:   
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         

Name                : Dnscache
RequiredServices    : {nsi, Tdx}
CanPauseAndContinue : False
CanShutdown         : False
CanStop             : True
DisplayName         : DNS Client
DependentServices   : {NcaSvc}
MachineName         : .
ServiceName         : Dnscache
ServicesDependedOn  : {nsi, Tdx}
ServiceHandle       : 
Status              : Running
ServiceType         : Win32ShareProcess
Site                : 
Container           : 

See how the properties from the service AND the file are both on the one object?


# Create two Arrays partial name 
# combine the two arrays

$Object1 = @()

# Create a PSObject with the associated properties hashtable and add to $userObj
$userObj= New-Object PSObject -Property @{
  name="Decvic-srv1"
  Share='$admin'
  Path="c:\temp"
  SizeKB=[math]::Round((200456789/1KB),2)
  Files="345"
  }

$Object1 += $userObj
$Object1

$Object2 = @()

# Create a PSObject with the associated properties hashtable and add to $userObj
$userObj= New-Object PSObject -Property @{
  name="Decvic-srv2"
  Share='$test'
  Path="c:\temp\robertp"
  SizeKB=[math]::Round((4567789/1KB),2)
  Files="1000"
  Desc="Secondary file that needs to have write acess"
  Function="1ST Function"
  }

$Object2 += $userObj
$Object2
$First = $Object1
$Second = $Object2

$Object3 = @()

# Get a list of the properties on both objects
$PropertyList1 = $Object1 | Get-Member -Type Properties | select -expand Name
$PropertyList2 = $Object2 | Get-Member -Type Properties | select -expand Name | 
Where- 
Object -FilterScript { $PropertyList1 -notcontains $PSItem.Name }

$userObj = New-Object PSObject

# Add the 1st object properties, from the second object, to the first object
foreach ($Property in $PropertyList1) {
#Write-Host ('Adding property: {0}' -f $Property)
Add-Member -InputObject $userObj -Name $Property -MemberType NoteProperty -Value 
$Object1.$($Property)
}

$Object3 += $userObj

$userObj = New-Object PSObject

# Add 2nd object the properties, from the second object, to the first object
foreach ($Property in $PropertyList2) {
# Write-Host ('Adding property: {0}' -f $Property)
Add-Member -InputObject $userObj -Name $Property -MemberType NoteProperty -Value 
$Object2.$($Property)
}

$Object3 += $userObj

# Output the object
$Object3 | select Desc, Files, Function, name, Path, Share, SizeKB | ft

Tags:

Powershell