Author Posts

April 17, 2017 at 4:19 pm

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  

April 18, 2017 at 5:30 pm

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

April 21, 2017 at 4:14 pm

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

April 24, 2017 at 5:35 pm

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.

April 24, 2017 at 8:12 pm

 [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