user2821260
user2821260

Reputation: 23

Passing Psobject Property values to another Psobject

I am using two PSObjects lets call them $job1 and $job2 that contain some identical Property Names. The properties for $job1 has the values but the matching properties for $job2 does not.I need to be able to do a foreach loop where if property name in $job1 matches $job2 property name, update Property value of $job2 based on the Property Name.

I was playing around with the below code but I cannot get it to update the Property values of $job2.

$job1.PSObject.Properties | %{if($_.Name -in $job2.PSObject.Properties.Name){[$_.Value = $job2.PSObject.Properties.Value}}

Thanks in advance for all the help!!

Upvotes: 1

Views: 822

Answers (1)

mjolinor
mjolinor

Reputation: 68273

Try this:

Foreach ( $Property in $Job1.Psobject.Properties.Name )
{
 Try { $Job2.$Property = $Job1.$Property }
 Catch { Continue }
}

Upvotes: 1

Related Questions