3

I thought this was simple. And I'm sure it is. But, I can't seem to crack it.

I have 3 functions that return a true or false value.

In a later if evaluation I am trying to logical or the 3 results together.

Something like this:

if (Fnc1 -or Fnc2 -or Fnc3) { write-host "Yes" }

Not only is Powershell highlighting the syntax differently for the first Fnc1 from the others, it's only returning true or false based on the value of Fnc1 from what I can tell.

I know this works:

if ((Fnc1 -eq $true) -or (Fnc2 -eq $true) -or (Fnc3 -eq $true)) { write-host "Yes" }

But, that seems like overkill and un-necessary.

What am I missing?

Appleoddity
  • 647
  • 1
  • 6
  • 21
  • 1
    For syntax highlighting I do agree. If you use `$(fnc2)` syntax highlighting is better. Nevertheless this is working with your first syntax. – Hazrelle Dec 15 '21 at 20:28
  • @Hazrelle hi. I am not getting the same result. If Fnc1 is true the whole thing evaluates true. If Fnc1 is false the whole thing evaluates false. – Appleoddity Dec 15 '21 at 20:38
  • Are you trying to understand why you must enclose your functions in `(..)` for the expression to be evaluated properly ? – Santiago Squarzon Dec 15 '21 at 20:42

2 Answers2

4

PowerShell attempts to parse the -or token as a function parameter when you place it after a function name like that. Surround each function call in ():

if ((Fnc1) -or (Fnc2) -or (Fnc3)) { write-host "Yes" }
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

another way to get that is to use the -contains collection operator. lookee ...

function Get-True {$True}
function Get-False {$False}


@((Get-True), (Get-False), (Get-True)) -contains $True

output = True

if all those were Get-False, the result would be False.


note that this method requires that all the function calls be run before the -contains operator can test anything. that means the -or solution would be more efficient since that would run each function call in sequence and stop when the -or was satisfied.

the -or solution can be much more efficient if the function calls take any significant amount of time or resources.

thanks to @SagePourpre for pointing that out. [grin]

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • OP specifically mentionned a **logical OR**. Your solution doesn't work under that pretense since a logical OR would stop processing the statements after the first instance of `$true`. Your solution do process all the statements regardless of if a `$true` result was obtained already. This will have a performance cost and potentially unintended side-effects if you did want to use it as a **logical OR** alternative. – Sage Pourpre Dec 16 '21 at 09:19
  • @SagePourpre - ah! that is a good point. yes, my solution would seem to require processing each function call before the `-contains` would be run. ///// should i add that to the Answer or does your comment do the job? – Lee_Dailey Dec 16 '21 at 13:24
  • Since you ask, I like to integrate relevant comments in my own answers. That way my answer become the full stand-alone picture instead of a "Compile your own answer" situation where you have an answer in an unoptimized state and need to read through the comments to get all the informations you need. I usually comment back to let the commenter I edited my answer in the form of a thanks of some sort. tl;dr : I want to craft the perfect answer so if you point out something that can help me achieve that, I will for sure edit my answer every time. – Sage Pourpre Dec 17 '21 at 05:57
  • 1
    @SagePourpre - thank you for the feedback. [*grin*] – Lee_Dailey Dec 17 '21 at 13:23