0

I need help to compare 2 variable values based on the 1st variable.

example-

$a= 
Name             VLanId
----             ------
VM Network       0
VL600_ACC        600
VL602_SEC2       602

$b=
Name               VLanId
----               ------
VM Network         0
VL600_ACC          600
VM-VLAN            102
VL602_SEC2         602

So in this case I want to compare $a with $b and if all the $a values are in $b then it should show pass else fail.

  • Thanks @MathiasR.Jessen I have tried this but it's not showing the correct output, I guess it's comparing values line by line – True Entertainer Aug 30 '22 at 11:46
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `$a |Join $b -on Name -where { $Left.VLanId -ne $Right.VLanId }` – iRon Aug 30 '22 at 12:49

1 Answers1

0

I would recommend using the Compare-Object CMDLet. Compare Object compares both of your input objects and result in a list of objects who are different. You take $a as your base and compares it to $b. If $comp is equal $null (no results found), then all objects of $a where found in $b.

$comp = Compare-Object $a.Name $b.Name

if ($comp -eq $null) {
Write-Host "All Objects of $a where found in $b: Pass"
} else {
Write-Host "Some Objects of $a where Not found in $b: Fail"
}
LKo.exp
  • 164
  • 7
  • That outputs `VM-VLAN` on the given example which should be excluded because it doesn't exist at all in `$b`, but this would probably work: `Compare-Object $a $b.where{$_.Name -in $a.Name} -Property VLanId` – iRon Aug 30 '22 at 12:51