Hello all and good morning.
I'm just looking to see if I can format the output of Compare-Object into a pscustomobject. I have taken a look at the following questions that are similar:
...but, I just don't seem to grasp the concept of it. I hate to ask questions that are already have been answered that correlate to this but, im just a bit confused. The 3rd link was the only one I was beginning to understand but, still got lost in the sauce.
Just looking to separate the output based off the SideIndicator
return property.
$Reference = Get-Content "C:\Users\Abe\Desktop\onj1.txt"
$Difference = Get-Content "C:\Users\Abe\Desktop\onj2.txt"
Compare-Object -ReferenceObject $Reference -DifferenceObject $Difference -IncludeEqual | Tee-Object -Variable CompareObject | Out-Null
$compareObject | Foreach {
$ONJ1 = $_.SideIndicator -eq "<=" | Select $_.InputObject
$ONJ2 = $_.SideIndicator -eq "=>" | Select $_.InputObject
$Both = $_.SideIndicator -eq "==" | Select $_.inputobject
[pscustomobject]@{
ONJ1 = $ONJ1
ONJ2 = $ONJ2
Both = $Both
}
}
Unfortunately, all 3 columns return the same output and im assuming its because im selecting the InputObject property but, it grabs them all.
EDIT: As explained by Zett42
$Reference = Get-Content "C:\Users\Abe\Desktop\onj1.txt"
$Difference = Get-Content "C:\Users\Abe\Desktop\onj2.txt"
Compare-Object -ReferenceObject $Reference -DifferenceObject $Difference -IncludeEqual | Tee-Object -Variable CompareObject | Out-Null
$null = [array]$ONJ1 = $compareObject | Where-Object {$_.SideIndicator -eq "<="} | Select-Object -ExpandProperty InputObject
$null = [array]$ONJ2 = $compareObject | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject
$null = [array]$Both = $compareObject | Where-Object {$_.SideIndicator -eq "=="} | Select-Object -ExpandProperty InputObject
For($i=0; $i -lt ($ONJ1.Count + $ONJ2.Count + $Both.Count); $i++){
[pscustomobject]@{
ONJ1 = $ONJ1[$i]
ONJ2 = $ONJ2[$i]
Both = $Both[$i]
}
}