我想知道如果有人输入错误或输入是/否以外的其他内容时如何重新提示 是必须将其创建为提示功能还是有更简单的方法?
$Continue = Read-Host -Prompt "Continue? Yes/No"
Switch($Continue){
'Yes' { Write-host -ForegroundColor Yellow "Moving on..." }
'No' {Write-host "...GoodBye"
Exit
}
default { #HOW TO RE-PROMPT USER FOR YES/No?
}
}
答案 0 :(得分:1)
使用While循环检查一个变量。如果键入是,则$Check
将等于$true
退出循环。否则它将保持为假,并再次运行循环。
$Check = $false
while($Check -eq $false){
Switch(Read-Host -Prompt "Continue? Yes/No"){
'Yes' {
Write-host -ForegroundColor Yellow "Moving on..."
$Check = $true
}
'No' {
Write-host "...GoodBye"
return
}
}
}