我正在为工作项目的菜单工作。以下代码正在处理一件事情。
如果代码已运行并且打开的窗口已关闭,无论是有意还是偶然,do
循环都会停止,我无法获得所需的结果。
我的目标是采用一种万无一失的方法来确保选择一个选项而不是绕过选项。
如果选择了选项1,则循环将根据需要停止。
如果选择了任何其他选项,则会再次提示您选择一个选项。
这告诉我do
循环正在运行。
我遗失了一些东西,并且无法找到答案,我无法找到答案。
function Choose_Site {
[int]$Script:Site_Check = "0"
$script:Site_Title = "Please Choose Your Site"
$script:Site_Info = "Please Choose Appropriate Site For This Machine"
$script:Site_Options = [System.Management.Automation.Host.ChoiceDescription[]] @("&1", "&2", "&3", "&4", "&5", "&A", "&B", "&C", "&D", "&E")
[int]$script:Default_Choice = 9
$script:Site_Menu = $Host.UI.PromptForChoice($script:Site_Title, $script:Site_Info, $script:Site_Options, $script:Default_Choice)
switch ($script:Site_Menu) {
0 {Write-Host "1" -ForegroundColor Green; $script:Site_Check = "1"}
1 {Write-Host "2" -ForegroundColor Green}
2 {Write-Host "3" -ForegroundColor Green}
3 {Write-Host "4" -ForegroundColor Green}
4 {Write-Host "5" -ForegroundColor Green}
5 {Write-Host "A" -ForegroundColor Green}
6 {Write-Host "B" -ForegroundColor Green}
7 {Write-Host "C" -ForegroundColor Green}
8 {Write-Host "D" -ForegroundColor Green}
9 {Write-Host "E" -ForegroundColor Green}
}
}
do {
Choose_Site
} until (
$Script:Site_Check -ne "0"
)
答案 0 :(得分:0)
如果您预期的值未设置,则可以使用Finally
的{{1}}块通过调用函数来执行您想要的操作:
Try..Catch
您的问题发生了,因为关闭对话框(我认为)终止了脚本。即使脚本终止,function Choose_Site {
[int]$Script:Site_Check = "0"
$script:Site_Title = "Please Choose Your Site"
$script:Site_Info = "Please Choose Appropriate Site For This Machine"
$script:Site_Options = [System.Management.Automation.Host.ChoiceDescription[]] @("&1", "&2", "&3", "&4", "&5", "&A", "&B", "&C", "&D", "&E")
[int]$script:Default_Choice = 9
Try {
$script:Site_Menu = $Host.UI.PromptForChoice($script:Site_Title, $script:Site_Info, $script:Site_Options, $script:Default_Choice)
switch ($script:Site_Menu) {
0 {Write-Host "1" -ForegroundColor Green; $script:Site_Check = "1"}
1 {Write-Host "2" -ForegroundColor Green}
2 {Write-Host "3" -ForegroundColor Green}
3 {Write-Host "4" -ForegroundColor Green}
4 {Write-Host "5" -ForegroundColor Green}
5 {Write-Host "A" -ForegroundColor Green}
6 {Write-Host "B" -ForegroundColor Green}
7 {Write-Host "C" -ForegroundColor Green}
8 {Write-Host "D" -ForegroundColor Green}
9 {Write-Host "E" -ForegroundColor Green}
}
} Finally {
if ($script:Site_Check -eq "0") { Choose_Site }
}
}
也会一直执行。