我正在编写一个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"}
}
答案 0 :(得分:2)
将菜单代码放入外部循环,除非$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 )