Private/Wissen/C_Advance/C03_Kontrollstrukturen.ps1

<#
 
# Kontrollstrukturen
 
Skriptausführung mit Bedingungen und Schleifen steuern
 
- **Hashtags** if switch while do until for foreach
- **Version** 2019.10.15
 
#>


# TODO Für die korrekte Syntax gibt es Schnipsel zum einfügen (CTRL + ALT + J)
# TODO ... oder über Autovervollständigung (Beachte Symbol und s.a. ex-... oder z.B. switch)

# ? if (condition) { } elseif (condition) { } else { }
Get-Help -Name about_if -ShowWindow

# ? switch
Get-Help -Name about_switch -ShowWindow

# ? while (condition) {} s. Kopfgesteuerte Endlosschleifen
Get-Help -Name about_while -ShowWindow

# ? do {} while (condition) s. Fußgesteuerte Endlosschleifen
Get-Help -Name about_do -ShowWindow

# ? do {} until (condition) s. Fußgesteuerte Endlosschleifen
Get-Help -Name about_do -ShowWindow

# ? for => s. Zählerschleife
Get-Help -Name about_for -ShowWindow

# ? foreach => s. Listenschleifen
Get-Help -Name about_foreach -ShowWindow

# - - - - - - -

foreach($prozess in (Get-Process)) {
     $prozess.Name
}
# ? vs.
Get-Process -PipelineVariable prozess | ForEach-Object -Process { $prozess.Name } # ps | % { $_.Name }

# ? Was ist besser? Bsp 1 | Bsp 2
# TODO Tippaufwand? 52 | 81 (18)
# TODO Lesbarkeit? Nein | JA <<< **Nichts geht über LESBARKEIT**
# TODO Erster Process-Name? Nein | JA
# TODO Gesamte Verarbeitungszeit? Schnell | Langsam

# ? break; continue
Get-Help -Name about_Break -ShowWindow
Get-Help -Name about_Continue -ShowWindow

$procs = Get-Process
foreach($item in $procs) {
    if($item.Name -eq "WUDFHost") {
        #continue
        # vs.
        break
    }
    $item.Name
}