Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: if items in array1 contain items in array2, what am I doing wrong here

I am not sure what I am doing wrong here:

    $ZertoGenericAlert =  "VRA0030"
    $ZvmToVraConnection = "ZVM0002"
    $ZvmToZvmConnection = "ZVM0003", "ZVM0004"

  $thoseerrors = "ZVM0002", "VPG0006", "VPG0004" , "ZVM0003", "ZVM0004"

  if ($thoseerrors -contains $ZvmToZvmConnection) {Echo "bingo"} Else {Echo "fail"}

It keeps coming as "fail" when I run that that entire piece of code

It gives me "Bingo" when ONLY 1 item is found in the $zvmtozvmconnection

Ie I remove "ZVM0004" and only "ZVM003" remains i get "Bingo"

I also tested -match and that did not work either

Please help

like image 360
AdilZ Avatar asked Sep 15 '26 14:09

AdilZ


1 Answers

-contains doesn't work that way. It checks if a single item is contained in an array. -in is the same, with the other order ($array -contains $item or $item -in $array).

You should use the Compare-Object cmdlet for this:

if ((Compare-Object -ReferenceObject $thoseerrors -DifferenceObject $ZvmToZvmConnection -ExcludeDifferent -IncludeEqual)) {
    'bingo'
} else {
    'fail'
}
like image 113
briantist Avatar answered Sep 17 '26 05:09

briantist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!