Menu Fucntion

This topic contains 4 replies, has 5 voices, and was last updated by Profile photo of Wilfredo Perez Wilfredo Perez 1 day, 6 hours ago.

  • Author
    Posts
  • #68785
    Profile photo of Chad Nathan Jones
    Chad Nathan Jones
    Participant

    Currently with this code I wrote, I'm not getting the statement to accept anything. It keeps firing the Else statement and isn't putting anything in $floorou

    $ouAnswer=""
     function mainmenu{  
     cls 
     echo "Please Select Office Location"
     echo "---------------------------------------------------------"  
     echo "    1. City1"  
     echo "    2. City2"  
     echo "    3. City3"  
     echo "    4. City4"  
     echo "    5. City5"
     echo "    6. City6"
     echo "---------------------------------------------------------"    
     $ouAnswer = read-host "Please Make a Selection"  
     if ($ouAnswer -eq 1){$floorou = "ou=Users,ou=City1,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"}  
     if ($ouAnswer -eq 5){$floorou = "ou=Users,ou=City5,dc=domain,dc=local"}  
     if ($ouAnswer -eq 6){$floorou = "ou=Users,ou=City6,dc=domain,dc=local"}  
     else {write-host -ForegroundColor red "Invalid Selection"  
           sleep 5  
           mainmenu  
          }  
                    }  
     mainmenu  
    
    
  • #68874
    Profile photo of Delonte Johnson
    Delonte Johnson
    Participant

    The code below should accomplish the task...

     function mainmenu
     {  
         cls 
         echo "Please Select Office Location"
         echo "---------------------------------------------------------"  
         echo "    1. City1"  
         echo "    2. City2"  
         echo "    3. City3"  
         echo "    4. City4"  
         echo "    5. City5"
         echo "    6. City6"
         echo "---------------------------------------------------------"    
         $ouAnswer = read-host "Please Make a Selection" 
         Switch($ouAnswer)
         {
            1 {$floorou = "ou=Users,ou=City1,dc=domain,dc=local"}
            2 {$floorou = "ou=Users,ou=City2,dc=domain,dc=local"}
            3 {$floorou = "ou=Users,ou=City3,dc=domain,dc=local"}
            4 {$floorou = "ou=Users,ou=City4,dc=domain,dc=local"}
            5 {$floorou = "ou=Users,ou=City5,dc=domain,dc=local"}
            6 {$floorou = "ou=Users,ou=City6,dc=domain,dc=local"}
         }
        
        If ($floorou)
        {
            $floorou
        }
        Else
        {
            write-host -ForegroundColor red "Invalid Selection"
        }
    }
    
    mainmenu
    
  • #69193

    This might be an overkill, but try this out, maybe you'll like it

    function DrawMenu {
        ## supportfunction to the Menu function below
        param ($menuItems, $menuPosition, $menuTitel)
        $fcolor = $host.UI.RawUI.ForegroundColor
        $bcolor = $host.UI.RawUI.BackgroundColor
        $l = $menuItems.length + 1
        cls
        $menuwidth = $menuTitel.length + 4
        Write-Host "`t" -NoNewLine
        Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
        Write-Host "`t" -NoNewLine
        Write-Host "* $menuTitel *" -fore $fcolor -back $bcolor
        Write-Host "`t" -NoNewLine
        Write-Host ("*" * $menuwidth) -fore $fcolor -back $bcolor
        Write-Host ""
        Write-debug "L: $l MenuItems: $menuItems MenuPosition: $menuposition"
        for ($i = 0; $i -le $l;$i++) {
            Write-Host "`t" -NoNewLine
            if ($i -eq $menuPosition) {
                Write-Host "$($menuItems[$i])" -fore $bcolor -back $fcolor
            } else {
                Write-Host "$($menuItems[$i])" -fore $fcolor -back $bcolor
            }
        }
    }
    
    function Show-Menu {
        ## Generate a small "DOS-like" menu.
        ## Choose a menuitem using up and down arrows, select by pressing ENTER
        param ([array]$menuItems, $menuTitel = "MENU")
        $vkeycode = 0
        $pos = 0
        DrawMenu $menuItems $pos $menuTitel
        While ($vkeycode -ne 13) {
            $press = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown")
            $vkeycode = $press.virtualkeycode
            Write-host "$($press.character)" -NoNewLine
            If ($vkeycode -eq 38) {$pos--}
            If ($vkeycode -eq 40) {$pos++}
    
            if ($pos -lt 0) {$pos = $menuItems.length -1}
    
           if ($pos -ge $menuItems.length) {$pos = 0}
            DrawMenu $menuItems $pos $menuTitel
        }
        Write-Output $($menuItems[$pos])
    }
    
    Function Enter-Menu{
        $MenuArray = "1. City","2. City", "3. City", "4. City", "5. City", "6. City", "Exit"
        $selection = Show-Menu $MenuArray "MAIN MENU"
        switch -Wildcard ($selection) {
            "1*" {Do-Something -floorou "ou=Users,ou=City1,dc=domain,dc=local"}
            "2*" {Do-Something -floorou "ou=Users,ou=City2,dc=domain,dc=local"}
            "3*" {Do-Something -floorou "ou=Users,ou=City3,dc=domain,dc=local"}
            "4*" {Do-Something -floorou "ou=Users,ou=City4,dc=domain,dc=local"}
            "5*" {Do-Something -floorou "ou=Users,ou=City5,dc=domain,dc=local"}
            "6*" {Do-Something -floorou "ou=Users,ou=City6,dc=domain,dc=local"}
            "Exit" { exit}
        }
    }
    
    Function Do-Something ($floorou) {
        If ($floorou)
        {
            $floorou
        }
        Else
        {
            Enter-Menu
        }
    }
    
    Enter-Menu
    
  • #69417
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    The issue with your code specifically is in your use of IF statements. If you were to input 6 you wouldn't get the "Invalid Selection" message because of the final IF/ELSE statement. To do it with IF statements, you would use IF/ELSEIF/ELSE

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

    The SWITCH statement is probably better for this, but I hope this will help you understand where your code went wrong.

  • #69430
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant
     [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
     $ouAnswer=""
     function mainmenu{  
     cls 
     echo "Please Select Office Location"
     echo "---------------------------------------------------------"  
     echo "    1. City1"  
     echo "    2. City2"  
     echo "    3. City3"  
     echo "    4. City4"  
     echo "    5. City5"
     echo "    6. City6"
     echo "---------------------------------------------------------"    
     [int]$ouAnswer = read-host "Please Make a Selection"  
     switch($ouAnswer){
     1{$floorou = "ou=Users,ou=City1,dc=domain,dc=local"}
     2{$floorou = "ou=Users,ou=City2,dc=domain,dc=local"}
     3{$floorou = "ou=Users,ou=City3,dc=domain,dc=local"}
     4{$floorou = "ou=Users,ou=City4,dc=domain,dc=local"}
     5{$floorou = "ou=Users,ou=City5,dc=domain,dc=local"}
     6{$floorou = "ou=Users,ou=City6,dc=domain,dc=local"}
     Default {[System.Windows.MessageBox]::Show("Invalid Selection", "OU", 0, 16 )}
        }
     }  
     mainmenu
    
    

You must be logged in to reply to this topic.