我正在尝试创建一个函数,其中用户必须提供一个不能包含空字符串的文件名。除此之外,字符串不能包含点。例如,当我运行此功能时,我会在输入“test”时不断循环。知道为什么吗?
function Export-Output {
do {
$exportInvoke = Read-Host "Do you want to export this output to a new .txt file? [Y/N]"
} until ($exportInvoke -eq "Y" -or "N")
if ($exportInvoke -eq "Y") {
do {
$script:newLog = Read-Host "Please enter a filename! (Exclude the extension)"
if ($script:newLog.Length -lt 1 -or $script:newLog -match ".*") {
Write-Host "Wrong input!" -for red
}
} while ($script:newLog.Length -lt 1 -or $script:newLog -match ".*")
ni "$script:workingDirectory\$script:newLog.txt" -Type file -Value $exportValue | Out-Null
}
}
编辑:
在相关说明中:
do {
$exportInvoke = Read-Host "Do you want to export this output to a new .txt file? [Y/N]"
} until ($exportInvoke -eq "Y" -or "N")
当我使用这些代码行时,我可以简单地按回车键来绕过Read-Host
. 当我"Y" -or "N"
简单地替换它时"Y"
,它不会。知道为什么会这样吗?