0

Having trouble getting my menus to work. I would like to make a menu class, but I'm stuck with v2. Each time I enter "1" at the "Main Menu", it will simply re-present me with the main menu instead of re-entering the function with the new $menuType.

function presentMenu{
param ([string]$menuType)

if($menuType = "Main"){
     Write-Host "MAIN MENU: 1.) Add Scanner 2.) Remove Scanner 3.) Lookup Scanner Config 4.) Exit"
     $command = Read-Host
     ##DEBUG## write-host $menuType

     if ($command -eq 1){
         presentMenu("addScaner")
     }
     elseif ($command -eq 2){

     }
     elseif ($command -eq 3){

     }
     elseif ($command -eq 4){
        exit
     }
     elseif ($command -eq 5){
        exit
     }
    else{
        presentMenu("Main")
    }
  }

elseif($menuType = "addScanner"){
    Write-Host "ADD SCANNER: 1.) From File 2.) From Input 3.) Back  4.) Exit"
    $command = Read-Host
    if ($command -eq 1){
        addScannerController("File")
    }
    if ($command -eq 2){
        addScannerController("Input")
    }
    if ($command -eq 3){
        presentMenu("Main")
    }
    if ($command -eq 4){
        exit
    }
    else{
        presentMenu("addScanner")
    }       
  }
}


try{
presentMenu("Main")
}
catch{
  Write-Error $_.Exception.ToString()
  Read-Host -Prompt "The above error occurred. Press Enter to exit."
}

The expected outcome when you enter "1" at the main menu, would be that you are presented with the "addScanner" menu. This really seems like a scope issue to me, but I can't seem to figure out how to correct it?

3
  • 2
    replace your call by presentMenu "addScanner" (without parenthesis). This is a typical gotcha when you start with powershell. No parenthesis for function calls. Commented Mar 30, 2017 at 11:56
  • 2
    Also consider using a switch statement rather than if..elseif..elseif.. Commented Mar 30, 2017 at 11:57
  • 1
    = is assignment, but not comparison. BTW, PowerShell is not functional language and, AFAIK, does not support tailcall optimization. Commented Mar 30, 2017 at 12:00

1 Answer 1

1

You are using = to compare the parameter, you need to use -eq.

You also have a typo, missing an n in addScanner.

Here's a stripped down example:

function presentMenu
{
    param ([string]$menuType)
    if($menuType -eq "Main")
    {
         Write-Host "MAIN MENU: 1.) Add Scanner 2.) Remove Scanner 3.) Lookup Scanner Config 4.) Exit"
         $command = Read-Host
         if ($command -eq 1)
         {
             presentMenu addScanner
         }
    }
    elseif($menuType -eq "addScanner")
    {
        Write-Host "ADD SCANNER: 1.) From File 2.) From Input 3.) Back  4.) Exit"
        $command = Read-Host
        if ($command -eq 1)
        {
            Write-Host "Scanner added"
        }
    }
}

presentMenu Main
Sign up to request clarification or add additional context in comments.

2 Comments

Palm to face Yes -eq NOT =. Can you tell I'm new to PS? :D
I do that all the time, occupational hazard of switching between languages!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.