1

I'm trying to verify the value of a variable, but no mater what, I can only get the right result if not using the -or

if (!$SER -eq "Y" -or  !$SER -eq "N"){
    write-host "ERROR: Wrong value for services restart" -foreground "red"
}

or like this

if (-not($SER -eq "Y") -or  -not($SER -eq "N")){
    write-host "ERROR: Wrong value for services restart" -foreground "red"
}

2 Answers 2

2

This works (ne stands for not equal):

if ($SER -ne "Y" -or $SER -ne "N") {
    Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}

This also works:

if ("Y", "N" -notcontains $SER) {
    Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}

And since PowerShell v3:

if ($SER -notin "Y", "N") {
    Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}

about_Comparison_Operators (v3)

Sign up to request clarification or add additional context in comments.

Comments

0

sodawillow provided a valid answer. Howevery, you can simplify this using -notin:

if ($SER -notin 'Y', 'N') {
    Write-Host "ERROR: Wrong value for services restart" -ForegroundColor Red
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.