The following if statements with -ne and -or operators is not working (Note I did not include the braces). $MyVariable is an integer coming in from a parameter in a function and having the if statement check the value. What I want is if $MyVariable is not equal to 16, 24 or 32 then return with error message otherwise continue. Calling the function as FunctionName 16; in the ps1 script.
Function is defined as follows:
function FunctionName
{
param([Parameter(Mandatory = $false, Position = 0)]
[AllowNull()]
[int] $MyVariable,
[Parameter(Mandatory = $false, Position = 1)]
[bool] $MyVariable2,
[Parameter(Mandatory = $false, Position = 2)]
[bool] $MyVariable3);
if (...
}
What am I doing wrong in the following if statements?
if ($MyVariable -ne 16 -or $MyVariable -ne 24 -or $MyVariable -ne 32)...
if (16 -ne $MyVariable -or 24 -ne $MyVariable -or 32 -ne $MyVariable)...
if (($MyVariable -ne 16) -or ($MyVariable -ne 24) -or ($MyVariable -ne 32))...
-necomparisons will always be true. Use-andinstead, or-eqfor the comparison