Advertisement
Lee_Dailey

Get-MenuChoice - 2017-04-02

Apr 2nd, 2017
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-MenuChoice
  2.     {
  3.     #region Comment Based Help [CBH]
  4.  
  5.     <#
  6.     .SYNOPSIS
  7.         Display a text menu & returns a valid response.
  8.     #>
  9.  
  10.     #endregion
  11.  
  12.     #region Parameters
  13.     # For some unknown reason, parameter help comments will not work without a CBH.
  14.     [CmdletBinding()]
  15.     Param
  16.         (
  17.         # Required string or string array of menu item[s] to display.
  18.         [Parameter(
  19.             Mandatory,
  20.             Position = 0
  21.             )]
  22.         [ValidateNotNullOrEmpty()]
  23.         [string[]]
  24.         $MenuItems,
  25.  
  26.         # Optional menu title.
  27.         [Parameter(
  28.             Position = 1
  29.             )]
  30.         [string]
  31.         $MenuTitle = '',
  32.  
  33.         # Optional user prompt.
  34.         [Parameter(
  35.             Position = 2
  36.             )]
  37.         [string]
  38.         $MenuPrompt = 'Please enter a choice from the above items '
  39.         )
  40.     #endregion
  41.  
  42.     #region Init variables
  43.     $ValidMenuChoices = $MenuItems | ForEach-Object {$_[0]}
  44.     $Frequency = 1000
  45.     $Duration = 100
  46.     $MenuChoice = ''
  47.     #endregion
  48.  
  49.     while ($MenuChoice -notin $ValidMenuChoices)
  50.         {
  51.         Clear-Host
  52.         if (-not [string]::IsNullOrEmpty($MenuTitle))
  53.             {
  54.             $MenuTitle | Out-Host
  55.             }
  56.         $MenuItems | Out-Host
  57.         Write-host ''
  58.         $MenuChoice = (Read-Host $MenuPrompt).ToLower()
  59.         if ($MenuChoice -notin $ValidMenuChoices)
  60.             {
  61.             # The bell char doesn't work in the ISE and [console]:: items
  62.             #    don't work in non-interactive sessions.
  63.             if ($Host.Name -match 'ISE')
  64.                 {
  65.                 [console]::Beep($Frequency, $Duration)
  66.                 }
  67.                 else
  68.                 {
  69.                 Write-Host [char]7
  70.                 }
  71.             }
  72.         }
  73.  
  74.     return $MenuChoice
  75.  
  76.     } # end function Get-MenuChoice
  77.  
  78.  
  79. #region Testing section - comment out or remove when finished
  80. $TopMenu = (
  81.     '1 - First item',
  82.     '2 - Second choice',
  83.     '3 - Third selection',
  84.     'x - Exit')
  85.  
  86.  
  87. #$Choice = Get-MenuChoice $TopMenu
  88. $Choice = Get-MenuChoice -MenuItems $TopMenu -MenuTitle 'Custom menu title' -MenuPrompt 'Custom menu prompt '
  89.  
  90. Write-Output ''
  91. switch ($Choice)
  92.     {
  93.     '1' {Write-Output "Do the first thing."; break}
  94.     '2' {Write-Output "The 2nd thing must be done."; break}
  95.     '3' {Write-Output "Three is the number of the thing to do."; break}
  96.     'x' {Write-Output "Exit now."; break}
  97.     }
  98. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement