菜单选择不会根据选择返回

Menu Selection not going back as per choice

我使用下面的简单代码在脚本中显示菜单

        Write-Host "============= Main Category ================================="
            Write-Host " '1' InfoBlox"
            Write-Host " '2' Installation"
            Write-Host " '3' Active Directory"
            Write-Host " '4' LogInventory"
            Write-Host " '5' Configuration"
            Write-Host " '6' New Server Build CMDB Update "
            Write-Host " '7' for Exit"
        Write-Host "========================================================"
        $choice = Read-Host "`nEnter Choice"

if($choice -eq '1')
{
#Menu for choice 
    cls
    Write-Host "=============InfoBlox================================="
    Write-Host " '1' Add CNAME record"
    Write-Host " '2' Delete CNAME record"
    Write-Host " '3' Add A-record and PTR record"
    Write-Host " '4' Delete A-record and PTR record"
    Write-Host " '5' for Main Menu"
    Write-Host "========================================================"
    $subchoice = Read-Host "`nEnter Choice"
#code here
  if($subchoice -eq '5')
        {
        cls
        Write-Host "============= Main Category ================================="
            Write-Host " '1' InfoBlox"
            Write-Host " '2' Installation"
            Write-Host " '3' Active Directory"
            Write-Host " '4' LogInventory"
            Write-Host " '5' Configuration"
            Write-Host " '6' New Server Build CMDB Update "
            Write-Host " '7' for Exit"
        Write-Host "========================================================"
        $choice = Read-Host "`nEnter Choice"
}
if($choice -eq '2')
{
 #code here
}
if($choice -eq '3')
{
 #code here

}

我面临的问题是当我第一次执行时它工作正常。例如当我选择 **1,2,3..** 没问题,但是当我再次从 3 返回到 1 作为选择时,脚本正在退出并显示 powershell 提示符.

请告诉我为什么会这样。 我可能无法正确解释。如有任何问题,请告诉我。

Mathias R. Jessen 在他的有用评论中给了你指针,每个菜单/选项都应该有自己的函数或脚本块,逻辑应该包含在一个无限循环中,当 选择退出选项。

出于演示目的,我已经减少了代码。

function Menu {
    Write-Host " '1' Option 1"
    Write-Host " '2' Option 2"
    Write-Host " '3' for Exit"
}

function SelectSomething {
    Read-Host "`nEnter Choice"
}

function FirstOption {
    Write-Host " '1' Add CNAME record"
    Write-Host " '2' Delete CNAME record"
    Write-Host " '3' for Main Menu"
    SelectSomething
}

function SecondOption {
    Write-Host " '1' InfoBlox"
    Write-Host " '2' Installation"
    Write-Host " '3' for Main Menu"
    SelectSomething
}

$firstScriptblock = {
    switch(FirstOption) {
        1 { 'Option 1 - Add CNAME record' }
        2 { 'Option 2 - Delete CNAME record' }
        3 { return }
        Default { Write-Warning 'Invalid Selection' }
    }
    $Host.UI.ReadLine()
    & $MyInvocation.MyCommand.ScriptBlock
}

$secondOption = {
    Write-Warning 'Not implemented! Back to Menu'
    $Host.UI.ReadLine()
}

:outer while($true) {
    Clear-Host
    Menu
    switch(SelectSomething) {
        1 { & $firstScriptblock }
        2 { & $secondOption }
        3 { break outer }
        Default { Write-Warning 'Invalid Selection' }
    }
}