@LaCosteGreen

Как оптимизировать скрипт PowerShell?

Добрый день. Начал изучение PowerShell.
Как можно проще описать "выбиралку"?
Пока придумал только такой вариант:
function swit{
$choice = Read-Host Введите номер

switch($choice){
    1{  
        
        Write-Host 1.Нажали 1 -ForegroundColor Green
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    2{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Green
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    3{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Green
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    4{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Green
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    5{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Green
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    6{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Green
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    7{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Green
        Write-Host 8.Нажали 8 -ForegroundColor Red
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    8{  Write-Host 1.Нажали 1 -ForegroundColor Red
        Write-Host 2.Нажали 2 -ForegroundColor Red
        Write-Host 3.Нажали 3 -ForegroundColor Red
        Write-Host 4.Нажали 4 -ForegroundColor Red
        Write-Host 5.Нажали 5 -ForegroundColor Red
        Write-Host 6.Нажали 6 -ForegroundColor Red
        Write-Host 7.Нажали 7 -ForegroundColor Red
        Write-Host 8.Нажали 8 -ForegroundColor Green
        Write-Host 9.Выход -ForegroundColor Red
        swit}
    9{  Write-Host ″Выход″; break}

    default {Write-Host ″Неверное значение, попробуйте еще раз.″ -ForegroundColor Magenta
    swit}
    }
} 
swit
  • Вопрос задан
  • 108 просмотров
Решения вопроса 1
sarapinit
@sarapinit
Точу водой камень
function swit {
    $choice = Read-Host 'Введите номер'

    if ($choice -eq 9) {
        Write-Host 'Выход'
    }
    elseif ($choice -ge 1 -and $choice -le 8) {
        for ($i = 1; $i -lt 9; $i++) {
            if ($i -eq $choice) {
                Write-Host "$i.Нажали $i" -ForegroundColor Green
            }
            else {
                Write-Host "$i.Нажали $i" -ForegroundColor Red
            }
        }
        swit
    }
    else {
        Write-Host 'Неверное значение, попробуйте еще раз.' -ForegroundColor Magenta
        swit
    }
}
swit
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы