I am having a issue trying to verify that if one or more items in a static array are in the results of a dynamic array that it simply output $true or $false.
I thought this would be pretty simply but I can’t get it to work for the life of me.
The contents of $Zone is an object that has a Name property. You can’t do a direct -like against the entire object. Try:
$zone.name -like “VerifyExists”
Instead. Or, in your Select, use Select-Object -Expand Name instead, which will make $ExistingZones a collection (not an array) of simple String objects.
However, if $VerifyExists is a collection, then -like can’t work. You would use -contains or -in instead, although those don’t accept wildcards.
Look at what you’re doing in the If statement. You’re not assigning $True to $ZoneExists, you’re comparing $ZoneExists to $True. Remove -eq and change it to =. -Eq is for comparing; = is for assigning.
Sorry Don, that was my typo/mistake. I copied a scratchpad version of what I was doing which I modified so it didn’t give any client info away and it still had the '{ZonesExist -eq $True). My working copy does use the = sign.
Then either $Zone or $VerifyExists may not contain what you think it does. Without running your code in your environment, I can’t get more specific - but if it were me, I’d write some “debug code” to output those variables, so that I could see what they actually contain.
Also, not knowing if this is your scratchpad thing again:
ForEach ($Zone in $ExistingZones) {
If ($Zone -like "$VerifyExists") {$ZonesExist -eq $True}}
Else {$ZonesExist = $False}
}
}
There are too many curly braces going on here. If you look, you’ve got an extra one after the $True, for example, and another extra one at the very end. That would change the behavior.
Finally, and again not knowing what your actual code looks like, you’re outputting $ZonesExist AFTER the END of the loop. That means the variable will only ever reflect the most recent comparison. You could have ten $False in a row and, with this logic, you’d never see them. If the 11th item was $True, that’ll you’d ever see.
Since that’s more compatible with older versions of PowerShell that don’t have the magic secret property enumeration trick introduced in v3 ;). Also note the reversal of logic to use the older -contains rather than -in - useful trick to know if you need to run on 1.0 or 2.0.