要求用户填充字段 (Powershell)



我在powershell中有一个片段,它从平面文件中读取问题并提示用户提供布尔或字符串响应(即你喜欢飞行汽车吗?你为什么喜欢跳伞?所有答案都写入.xls,以便稍后使用到数据库。

我可以让脚本为未选择布尔答案(即 A、B、C 或"是"、"否"(的用户重复问题。但是,让用户提供字符串(简短(答案要棘手一些。

$Question7 = Get-Content -path $PSScriptRootsrcQuestion7.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"
}

但是,如果变量$reason_for_hobby之前未初始化,则最好是do-while循环(执行此操作一次并在$reason_for_hobby少于 215 个字符时重复(:

do{
$reason_for_hobby = Read-Host -Prompt "Please write in the answer"
}while ($reason_for_hobby.Length -lt 215)

最新更新