Author Posts

April 18, 2017 at 1:11 pm

Below is my script for giving some menu options to a user but I always get the Else statement with "No Valid Selection"

function mainmenu{  
 echo "Please Select Office Location"
 echo "---------------------------------------------------------"  
 echo "    1. City"  
 echo "    2. City2"  
 echo "    3. City3"  
 echo "    4. city4"  
 echo "---------------------------------------------------------"    
 $ouAnswer = read-host "Please Make a Selection"  
 if ($ouAnswer -eq 1){$floorou = "ou=Users,ou=City,dc=domain,dc=local"}  
 if ($ouAnswer -eq 2){$floorou = "ou=Users,ou=City2,dc=domain,dc=local"}  
 if ($ouAnswer -eq 3){$floorou = "ou=Users,ou=City3,dc=domain,dc=local"}
 if ($ouAnswer -eq 4){$floorou = "ou=Users,ou=City4,dc=domain,dc=local"}
 else {write-host -ForegroundColor red "Invalid Selection"  
       sleep 5  

April 18, 2017 at 1:20 pm

You're using the construct incorrectly, unfortunately. The Else is only coupled with the final If, so if that If isn't true, the Else runs.

You probably want to be using the Switch construct instead.