L
L
LaCosteGreen2020-12-30 17:26:08
PowerShell
LaCosteGreen, 2020-12-30 17:26:08

How to optimize a PowerShell script?

Good afternoon. Started learning PowerShell.
What is the simplest way to describe "selection"?
So far I've come up with this option:

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2020-12-30
@LaCosteGreen

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question