When sharing scripts that can do multiple operations with other teams I find that adding a simple operator menu that presents a list of choices makes it easier for folks to use the scripts. Here is the simple menu that i add to a lot of my scripts and a quick explanation of how it works.
The menu is created using a PowerShell Function called Menu (This can be anything really but i like to keep it simple!). Within the function we start with a Clear-Host – this will ensure that the menu starts on a clean PowerShell session screen.
In between lines 6 and 17 below you can edit any of the text in the quotes to change the menus item or heading text
Within each menu item you need to callout the PowerShell functions that you want to run as part of the menu option. You can list them one per line as in my example below or on one line with a ; between them (e.g. CreateVMSnapshot; anyKey)
To have the menu launch when the PowerShell script is executed just put the name of the menu function at the bottom of the script outside of any function, see line 53
For more information on the anyKey function go here
EDIT: Modified the exit option to be Q for Quit to keep all menus consistent. Thanks to SiliconBrian for the suggestion!
Function Menu { Clear-Host Do { Clear-Host Write-Host -Object 'Please choose an option' Write-Host -Object '**********************' Write-Host -Object 'Snapshot VM Options' -ForegroundColor Yellow Write-Host -Object '**********************' Write-Host -Object '1. Snapshot VMs ' Write-Host -Object '' Write-Host -Object '2. Revert to Last Snapshot ' Write-Host -Object '' Write-Host -Object '3. Revert To Specific Snapshot ' Write-Host -Object '' Write-Host -Object 'Q. Quit' Write-Host -Object $errout $Menu = Read-Host -Prompt '(0-3 or Q to Quit)' switch ($Menu) { 1 { CreateVMSnapshot anyKey } 2 { RevertLastVMSnapshot anyKey } 3 { RevertSpecificVMSnapshot anyKey } Q { Exit } default { $errout = 'Invalid option please try again........Try 0-3 or Q only' } } } until ($Menu -eq 'q') } # Launch The Menu Menu
From a UX/UI perspective, may I suggest using ‘0’/’B’ack/’Q’uit/’M’ain type options for navigating between menus… And use 1/2/3/4/……. For actual options. Having one menu use 4 to quit and another use 9 and…. Gets confusing… Also, if you later add more options, ‘4’ Exit becomes 5 or 6, etc…
Brian
Cheers for the suggestion Brian! Will update the post
great work! Tks for the tips!!!