在我的脚本中,我需要确认使用Y或N来保存文件。但是,它被卡在一个循环中。
$ErrorOccured8 = $false
$confirmation = Read-Host "Do you want to save the output to text file? [y/n]"
while ($confirmation -ne "n") {
if ($confirmation -eq 'y') {
try {
$ErrorActionPreference = 'Stop'
Write-Host ("`Currently saving output to text. Please locate in temp folder.`n") -ForegroundColor darkmagenta
c:\temp\gsd.exe > c:\temp\$($server)_$(((Get-Date).ToUniversalTime()).ToString("yyyyMMdd")).txt
pause
} catch {
Write-Host ("`There has been some error. Kindly locate the file and refresh.`n") -ForegroundColor darkmagenta
$ErrorOccured8 = $true
}
} else {
exit
}
#$confirmation = Read-Host "Do you want to save the output to text file? [y/n]"
pause
}
pause
exit
我建议删除while循环并设置一个开关。
你的while循环只是不断尝试一遍又一遍地运行If
语句。
$ErrorOccured8 = $false
$confirmation = Read-Host "Do you want to save the output to text file? [y/n]"
switch ($confirmation) {
"y"
{
try {
$ErrorActionPreference = 'Stop'
Write-Host("`Currently saving output to text. Please locate in temp folder.`n") -ForegroundColor darkmagenta
c:\temp\gsd.exe > "c:\temp\$($server)_$(((get-date).ToUniversalTime()).ToString("yyyyMMdd")).txt"
} catch {
Write-Host("`There has been some error. Kindly locate the file and refresh.`n") -ForegroundColor darkmagenta
$ErrorOccured8=$true
}
}
"n"
{
exit
}
}