mercredi 16 octobre 2019

PowerShell Comparison Trouble, can't make compound comparisons

I am having problems making a compound comparison. I have not been successful at using -ne or -or. Below is how I think it should work:

If (($Var -ne 'Y') -or ($Var -ne 'N') {
     #Logic for error message and loop back to user promt
Else { #Go to another function }

Now when I try this, no matter what I put in, I go to my error message and since that loops back around to the user prompt it just makes an infinite loop. Even if I put in Y or N, I go to the error message.

Now I've broken that code down to the below code, and that works, but I'd much rather do a compound comparison where If the variable is neither "y" or "n", go to error (this is because inputs Y and N both progress the script to the same part of my code).

If ($Var -eq 'Y') {
     #Do action 1
}
ElseIf ($Var -eq 'N') {
     #Do action 1
}
Else {
     #Give error message and loop back to prompt
}

Aucun commentaire:

Enregistrer un commentaire