powershell - 如何在 PowerShell 中的函数结束时返回用户菜单

标签 powershell user-interface

我正在编写一个 PowerShell 脚本,它为用户提供了多个可供选择的选项。选择一个选项后,将执行一个功能,然后用户将返回到原始菜单。

在下面的代码中,为用户提供了选项,并执行了功能,但完成后,脚本就结束了。相反,我想要的是在函数结束时返回到原始用户菜单。有人可以告诉我该怎么做吗?谢谢!

function firstFunction {
    Write-Host "You chose option 1"
    return
}

Write-Host "Welcome"

[int]$userMenuChoice = 0
while ( $userMenuChoice -lt 1 -or $userMenuChoice -gt 4){
    Write-Host "1. Menu Option 1"
    Write-Host "2. Menu Option 2"
    Write-Host "3. Menu Option 3"
    Write-Host "4. Quit and Exit"

[int]$userMenuChoice = Read-Host "Please choose an option"}
switch ($userMenuChoice) {
    1{firstFunction}
    2{Write-Host "You chose option 2"}
    3{Write-Host "You chose option 3"}
default {Write-Host "Nothing selected"}
}

最佳答案

将菜单代码放入一个不会终止的外部循环中,除非 $userMenuChoice 为 4:

do {
  [int]$userMenuChoice = 0
  while ( $userMenuChoice -lt 1 -or $userMenuChoice -gt 4) {
    Write-Host "1. Menu Option 1"
    Write-Host "2. Menu Option 2"
    Write-Host "3. Menu Option 3"
    Write-Host "4. Quit and Exit"

    [int]$userMenuChoice = Read-Host "Please choose an option"

    switch ($userMenuChoice) {
      1{firstFunction}
      2{Write-Host "You chose option 2"}
      3{Write-Host "You chose option 3"}
      default {Write-Host "Nothing selected"}
    }
  }
} while ( $userMenuChoice -ne 4 )

关于powershell - 如何在 PowerShell 中的函数结束时返回用户菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625086/

相关文章:

user-interface - 我应该学习什么语言来创建命令行脚本和 GUI?

azure - 如何从 powershell 获取 EventHub 命名空间 SAS key

Python - PyQT4如何检测窗口中任意位置的鼠标点击位置?

powershell - PowerShell 中的递归通配符

powershell - 在脚本 block 调用中设置$ _

user-interface - Office 2007 UI 背后的原因

Python Tkinter 小窗口在主窗口之前弹出

python - PyQt:添加选项卡

powershell - 从字节转换为GB或MB不会返回任何内容

powershell - Entrypoint的exec形式通过shell执行