Combing 2 objects

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Combing 2 objects

Post by sekou2331 »

Hi I have 2 custom PS objects let’s say objA and objB with multiple members in each custom object. The only thing the same about each object is the computer name. Foreach computer name match I want to take one member from objB and add it to objA. The only issue is it is adding everything into that one column. I tried a foreach but it does the same thing. I am thinking I have to iterate through one of the objects.

Code: Select all

	if (objA.ComputerName -eq objB.ComputerName) {
		$m_Array = @()
		$m_Object = New-Object System.Object
		$m_Object | Add-Member -type NoteProperty -name ComputerName -Value $objA.ComputerName
		$m_Object | Add-Member -type NoteProperty -name Soruce -Value $objA.Soruce
		$m_Object | Add-Member -type NoteProperty -name Port -Value $objA.Port
		$m_Object | Add-Member -type NoteProperty -name Path2 -Value $objA.Path2
		$m_Object | Add-Member -type NoteProperty -name Path2 -Value $objA.Path3
		$m_Object | Add-Member -type NoteProperty -name Path1 -Value objB.path1
		
		$m_Array += $m_Object
		
		}
		
		$m_Array

jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combing 2 objects

Post by jvierra »

This is easier and more reliable:

Code: Select all

[array]$m_Array = if (objA.ComputerName -eq objB.ComputerName) {
	[pscustomobject]@{
		ComputerName = $objA.ComputerName
		Soruce = $objA.Soruce
		Port   = $objA.Port
		Path1  = $objB.path1
		Path2  = $objA.Path2
		Path3  = $objA.Path3
	}
}

$m_Array
You cannot have two properties with the same name.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Combing 2 objects

Post by sekou2331 »

:( This adding everything into one memeber. I just need to take what is ever matching by computer name between ObjA and ObjB and add the missing member to ObjA from ObjB.


ObjA:
ComputerName : computer1
name : A
numbers : 1000
File2 : Path2
File3 : Path3

ObjB:
ComputerName : computer1
Letter : b
Soruce : 1000
File1 : Path1



memeber added to ObjA from ObjB:
ComputerName : computer1
name : A
numbers : 1000
File2 : Path2
File3 : Path3
File1 : Path1
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combing 2 objects

Post by jvierra »

That is what the code I posted does.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Combing 2 objects

Post by sekou2331 »

I see the problem. There is multiple of ObjA. So it is putting everything in one object. instead of separating and adding the one member.




ComputerName : {Computername1, ComputerName2,}
name : {A, b, }
numbers : {1000, 100,}
File2 : {Path2, Path4}
File3 : {Path3,Path5}
File1 : {Path1 ,Path1 }

ObjA:
ComputerName : computer1
name : A
numbers : 1000
File2 : Path2
File3 : Path3

ObjA:
ComputerName : computer2
name : b
numbers : 100
File2 : Path4
File3 : Path5
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combing 2 objects

Post by jvierra »

You have to have an accurate representation of what you are trying to do. The code you posted was for two single objects. If there are multiples then you will have to enumerate them correctly.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Combing 2 objects

Post by sekou2331 »

So would I use GetEnumerator() to go though each?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combing 2 objects

Post by jvierra »

help foreach-object -online
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Combing 2 objects

Post by sekou2331 »

I think I figured this out. I don't know if this is the powershell way but what I used was a nested foreach on each object.

Code: Select all


foreach($objA in $objAs){
    foreach($objB in $objBs){
        if($objA.ComputerName -contains $objB.ComputerName){
          [pscustomobject]@{
            ComputerName  = $objA.ComputerName
            Soruce     = $objA.Soruce
	    Port   	   = $objA.Port
            Path1	   = $objB.Path1
	    Path2  	   = $objA.Path2
	    Path3       = $objA.Path3
          }
        
        }
    
      } 

    }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combing 2 objects

Post by jvierra »

That would not work.
  1. foreach ($objA in $objAs) {
  2.     if($objB = $objBs | where{$_.ComputerName -eq $objA.ComputerName}){
  3.         [pscustomobject]@{
  4.             ComputerName   = $objA.ComputerName
  5.             Soruce         = $objA.Soruce
  6.             Port           = $objA.Port
  7.             Path1          = $objB.Path1
  8.             Path2          = $objA.Path2
  9.             Path3          = $objA.Path3
  10.         }
  11.     } else {
  12.         Write-Host ('Object not found ' + $objA.ComputerName)
  13.     }
  14. }
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked