Functions/Write-TextMenu.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
function Write-TextMenu { <# .SYNOPSIS Creates the logic for a new simple text based menu. Originally published as script New-TextMenu in the PowerShellGallery .DESCRIPTION Creates the logic for a new simple text based menu. Originally published as script New-TextMenu in the PowerShellGallery .PARAMETER Option An array of [string] indicating the menu options. If you need to create a menu with a single option enclose the option in @(). .PARAMETER Title The title of the menu. Defaults to 'Menu Title'. .PARAMETER VariableName The name of the choice variable. Defaults to 'Choice'. .PARAMETER TestMenu If this switch is enabled then the menu is saved to a temporary file and run. .PARAMETER Clipboard If this switch is enabled then the menu logic is copied to the clipboard. .NOTES * The resulting output is a relatively small string array so the shorthand way of adding to an array ( += ) is used. * Removed extraneous temporary file. * Cleaned up formatting. * Only set of parameters so removing ParameterSetName and DefaultParameterSetName .EXAMPLE Write-TextMenu -Title 'Menu Title' -Option 'One', 'Two', 'Three' Creates a 3 option menu. .EXAMPLE Write-TextMenu -Title 'My Menu' -Option 'One' -VariableName 'Choice2' Creates a 1 option menu that looks like: $Choice2 = '' while ($Choice2 -ne 'q') { Write-Host 'My Menu' Write-Host '=======' Write-Host ' ' Write-Host '1 - One' Write-Host 'Q - Quit' Write-Host ' ' $Choice2 = Read-Host 'Selection' switch ($Choice2) { q { 'Exit message and code' } 1 { 'Option 1 code' } default { Write-Host 'Please enter a valid selection' } } } .EXAMPLE Write-TextMenu -Option 'Uno' Creates a 1 option menu that looks like: $Choice = '' while ($Choice -ne 'q') { Write-Host 'Menu Title' Write-Host '==========' Write-Host ' ' Write-Host '1 - Uno' Write-Host 'Q - Quit' Write-Host ' ' $Choice = Read-Host 'Selection' switch ($Choice) { q { 'Exit message and code' } 1 { 'Option 1 code' } default { Write-Host 'Please enter a valid selection' } } } .OUTPUTS [string[]] .LINK about_while Write-Host Read-Host about_switch #> #region Parameter [cmdletbinding(ConfirmImpact = 'Low')] [OutputType([string[]])] Param( [Parameter(Mandatory, HelpMessage = 'Enter the text of the menu option.', Position = 0, ValueFromPipeline = $True)] [string[]] $Option, [Parameter(Position = 1)] [string] $Title = 'Menu Title', [Parameter(Position = 2)] [string] $VariableName = 'Choice', [Parameter()] [switch] $TestMenu, [Parameter()] [switch] $Clipboard ) #endregion Parameter begin { Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]" $Result = [System.Collections.ArrayList]::new() } process { $null = $Result.Add("`$$VariableName = ''") $null = $Result.Add("while (`$$VariableName -ne 'q') {") $null = $Result.Add(" Write-Host '$title'") $null = $Result.Add(" Write-Host '$('='*$title.length)'") $null = $Result.Add(" Write-Host ' '") for ($i = 0; $i -lt $option.count; $i++) { $null = $Result.Add(" Write-Host '$($i+1) - $($option[$i])'") } $null = $Result.Add(" Write-Host 'Q - Quit'") $null = $Result.Add(" Write-Host ' '") $null = $Result.Add(" `$$VariableName = Read-Host 'Selection'") $null = $Result.Add(" switch (`$$VariableName) {") $null = $Result.Add(" q { 'Exit message and code' }") #$Result += " break }" for ($i = 0; $i -lt $option.count; $i++) { $null = $Result.Add(" $($i+1) { 'Option $($i+1) code' }") } $null = $Result.Add(" default { Write-Host 'Please enter a valid selection' }") $null = $Result.Add(' }') $null = $Result.Add('}') } end { if ($TestMenu) { $tempFilename = New-TemporaryFile Remove-Item -Path $tempFilename $tempFilename = $tempFilename.Directory.ToString() + '\' + $tempFilename.BaseName + '.ps1' $Result | Out-File -FilePath $tempFilename & $tempFilename Remove-Item -Path $tempFilename } else { Write-Output -InputObject $Result } if ($Clipboard) { $Result | clip.exe Write-Warning -Message 'Menu logic copied to clipboard' } Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]" } } |