function MyCustomMenue { param ( [string]$Title = 'Select Options' ) cls Write-Host "================ $Title ================" Write-Host "1: Press '1' for this option." Write-Host "2: Press '2' for this option." Write-Host "3: Press '3' for this option." Write-Host "Q: Press 'Q' to quit." }Danach kann diese Funktion einfach mittels
MyCustomMenue"geladen" werden. Da aber nun noch nicht die Eingabe des Users abgefragt wird, werden noch zusätzlich folgende zeilen angefügt.
Write-Host Write-Host "================================" $input = Read-Host "Please make a selection" cls Write-Host "================================" Write-HostBei der Zeile $input = Read-Host "Please make a selection" bleibt somit das Powershell-Script stehen und es wird auf eine Eingabe des Users gewartet, welche wiederum in der Variable $input gespeichert wird.
Da nun eigentlich das Script weiterlaufen würde fehlt noch die Logik, welche entscheidet ob die Eingabe des Users korrekt ist. Dies lässt sich am einfachsten mit einem sog. SWITCH erreichen.
switch ($input) { '1' { Write-Host "Selected Option 1" #Add Code here } '2' { Write-Host "Selected Option 2" #Add Code here } '3' { Write-Host "Selected Option 3" #Add Code here } '4' { Write-Host "Selected Option 4" #Add Code here } '5' { Write-Host "Selected Option 5" #Add Code here } 'q' { exit } default { #Add CODE witch is run if the user enters a wrong number exit } }Wichtig ist hierbei nicht auf den Punkt default zu vergessen, welcher sorge trägt, dass bei einer falschen Eingabe zumindest (in unserem Beispiel) das Powershell-Script beendet wird. Hier kann natürlich auch zB wieder das Menü gezeigt werden oder ein Standardfall angenommen werden.
Zum Abschluss nun das komplette Script:
function MyCustomMenue { param ( [string]$Title = 'Select Options' ) cls Write-Host "================ $Title ================" Write-Host "1: Press '1' for this option." Write-Host "2: Press '2' for this option." Write-Host "3: Press '3' for this option." Write-Host "Q: Press 'Q' to quit." } MyCustomMenue Write-Host Write-Host "================================" $input = Read-Host "Please make a selection" cls Write-Host "================================" Write-Host switch ($input) { '1' { Write-Host "Selected Option 1" #Add Code here } '2' { Write-Host "Selected Option 2" #Add Code here } '3' { Write-Host "Selected Option 3" #Add Code here } '4' { Write-Host "Selected Option 4" #Add Code here } '5' { Write-Host "Selected Option 5" #Add Code here } 'q' { exit } default { #Add CODE witch is run if the user enters a wrong number exit } }Die Ausgabe sieht in etwa so aus:
================ Select Options ================ 1: Press '1' for this option. 2: Press '2' for this option. 3: Press '3' for this option. Q: Press 'Q' to quit. ================================ Please make a selection:und nach zB. der Auswahl der Option 1:
================================ Selected Option 1