Запросить подтверждение сохранения файла с Y или N. Однако он застрял в цикле - PullRequest
0 голосов
/ 03 сентября 2018

В моем скрипте мне нужно запросить подтверждение для сохранения файла с помощью 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

1 Ответ

0 голосов
/ 03 сентября 2018

Я бы порекомендовал удалить цикл 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
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...