我在 powershell 中有一个片段,它从一个平面文件中读取问题,并提示用户提供一个布尔或字符串响应(例如,你喜欢飞车吗? 为什么你喜欢跳伞?所有的答案都会被写入一个.xls中,以便以后消耗到DB中。
我可以让脚本对没有选择布尔答案的用户重复提问(即A、B、C或 "是"、"否")。然而,让用户提供一个字符串(短)的答案是一个比较棘手的问题。
$Question7 = Get-Content -path $PSScriptRoot\src\Question7.txt -raw
Write-Host $Question7 -ForegroundColor Yellow
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
Writ-Host "Answer: $reason_for_hobby" -ForegroundColor Green
Add-Member -inputObject $infoObject -memberType NoteProperty -name "HBYREASON" -
value $reason_for_hobby
我想知道如何强制用户提供至少215个字符的答案,如果没有提供,则重复问题。
谢谢,祝你平安。
你可以通过一个简单的 while
-循环(当 $reason_for_hobby
有少于215个字符重复)。)
while ($reason_for_hobby.Length -lt 215){
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
}
然而,更好的将是一个 do-while
-循环,如果变量 $reason_for_hobby
之前没有被初始化(做一次,然后重复,而 $reason_for_hobby
有少于215个字符)。)
do{
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
}while ($reason_for_hobby.Length -lt 215)