When I do a left join on two objects via Powershell I define a property 'Name'. I want the property to have the value of the 'Name' field in the right object if the record exists and the value of the 'Name' field in the left object if it does not.
I have tried various conditional statements like
@{ Name = If($Right.Length -eq 0){'Left.Name'}else{'Right.Name'}}
@{ Name = If($Right -eq $null){'Left.Name'}else{'Right.Name'}}
@{ Name = If($null -eq $Right){'Left.Name'}else{'Right.Name'}}
but every conditional statement I've tried either always evaluates true or always evaluates false.
Obviously, I'm missing something. How do I do this?
Begin edit:
$objectA = @(
@{
"Id"="1"
"Name"="Bob"
},
@{
"Id"="2"
"Name"="Bill"
},
@{
"Id"="3"
"Name"="Ted"
}
)
$objectB = @(
@{
"Id"="2"
"Name"="John"
}
)
$objectA | leftjoin $objectB -On Id -Property @{ID = 'Left.ID'}, @{Name = If($Right -ne $null){'Left.Name'}else{'Right.Name'}}|Format-Table
I want the output to look like:
ID Name
-- ----
1 Bob
2 John
3 Ted
Unclear what
leftjoinis but assuming it works similarly to calculated properties forSelect-Objectthis does the trick: