When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
when sharing any code please remember to format it as code.
You can use the “Preformatted Text” button from the toolbar. Guide to Posting Code
from a quick glance at the code you said “for me both functions are equivalent”. What do you mean by this precisely as both functions are not equivalent.
Your first function is comparing string text.
Your second function is casting the variables as booleans by putting a ! in front of them, and also flipping the boolean so if the variable contains anything it’s treated as $false. At that point it’s no longer about what value $tst1 has, just that it does have value. From looking at your tests it always has value so !$tst1 will always be equivalent to $false.
Same thing for the second half of your condition in evaluate_2: as long as $tst1 has value your -eq comparison will return $false.
so we’ve essentially got:
($false) -and ($false)
false and false equals false, so we can shorten that to just $false.
but then you wrapped that and put another not in front of it
You know, you can edit your existing post and fix the formatting. You didn’t have to create a new one.
And BTW: I highly recommend to read
And BTW #2: Your function evaluate_2 is still wrong … or at least it’s not doing what you probably think it does. You may carefully read Courtneys reply again.
oh I should have clarified I have no idea what boolean algebra is. I looked it up and after a brief read I think you’re correct, what you’re trying to do is boolean algebra, but evaluate_2 is still wrong.
function evaluate_2
{
if (!((!$tst1 -eq 'a') -and (!$tst1 -eq 'b'))){
Write-Output 'success_2';
}
}
When you put ! (-not operator) in front of a variable definition it simply converts that object to a boolean. Example
PS> $tst1 = 'a'
PS> $tst1
a
PS> !$tst1
false
$tst1 is a string object. Casting it explicitly as a boolean would simply look at it and say “is there value here?” and return true. The opposite, from the -not/! operator would return false.
In your function however you’re trying to do a comparison of the value of $tst1 but by putting the -not operator right up against your variable you’ve essentially changed your comparison to false -eq 'b' which will never be true.
I’m not sure I know the exact outcome you’re trying to go for in evaluate_2 but would a -ne comparison be easier?
function evaluate_2 {
if (($tst1 -ne 'a') -and ($tst1 -ne 'b')) {
Write-Output 'success_2'
}
}
This is testing to see if $tst1 is not ‘a’ and not ‘b’ in which case it will evaluate as true.