Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-MenuChoice
- {
- [CmdletBinding ()]
- Param
- (
- [Parameter (
- Mandatory,
- Position = 0
- )]
- [string[]]
- $MenuList,
- [Parameter (
- Position = 1
- )]
- [string]
- $Title,
- [Parameter (
- Position = 2
- )]
- [string]
- $Prompt = 'Please enter a number from the above list or "x" to exit '
- )
- $ValidChoices = 0..$MenuList.GetUpperBound(0) + 'x'
- $Choice = ''
- while ([string]::IsNullOrEmpty($Choice))
- {
- Write-Host $Title
- foreach ($Index in 0..$MenuList.GetUpperBound(0))
- {
- Write-Host ('{0} - {1}' -f $Index, $MenuList[$Index])
- }
- $Choice = Read-Host -Prompt $Prompt
- Write-Host ''
- if ($Choice -notin $ValidChoices)
- {
- [System.Console]::Beep(1000, 300)
- Write-Warning ''
- Write-Warning (' [ {0} ] is not a valid selection.' -f $Choice)
- Write-Warning ' Please try again.'
- Write-Warning ''
- $Choice = ''
- pause
- }
- }
- # send it out to the caller
- if ($Choice -eq 'x')
- {
- 'Exit'
- }
- else
- {
- $Choice
- }
- } # end >>> function Get-MenuChoice
- '***** demo usage below *****'
- $MenuList = @(
- 'An Item'
- 'Some Other Item'
- 'Middle Menu Item'
- 'Yet Another Item'
- 'The Last Choice'
- )
- $Choice = Get-MenuChoice -MenuList $MenuList
- 'You chose [ {0} ] giving you [ {1} ].' -f $Choice, $MenuList[$Choice]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement