My custom user input validation method is failing to work as expected.
Here is the basic idea of the script:
$answer = Read-Host "Are you sure you wish to continue? [Y]es or [N]o"
while (!$answer -AND $answer -ine "y" -AND $answer -ine "yes" -AND $answer -ine "n" -AND $answer -ine "no") {
$answer = Read-Host "You hand entered an invalid response.`nPlease enter [Y]es or [N]o"
}
if ($answer -ieq "yes" -OR $answer -ieq "y") {
do action
}
else {
don't do action
}
The problem is that it only confirms if the string is empty/null and all other inputs will have the script exit the WHILE loop and proceed to the IF-ELSE statements.
Why?
Example:
If I input "yes", "YES", "Yes", "y", "Y" or any other similar variation to this, the application will skip/exit the WHILE statement and proceed to the IF statement, performing the desired action as intended. If I do not input anything (leaving the string null/empty), it will stay inside the While loop as intended. However, any other input will cause the application to skip/exit the WHILE statement and proceed to the ELSE statement, which is not what I want.
I have tried this using -eq, -ieq, -ceq, -like, -ilike, -ne, -ine, -notlike, and -inotlike I have even been playing around with the -AND and -OR operators in hopes that maybe I messed up on that setup.
Unfortunately, all gave me the same results.