Private/Show-Menu.ps1
|
<#
.SYNOPSIS Displays a menu and returns user selection .DESCRIPTION Shows a menu with options and returns the selected option number .PARAMETER Title Menu title .PARAMETER Options Array of menu options .PARAMETER ShowBackOption Whether to show back/exit option (0) .OUTPUTS Integer representing the selected option #> function Show-Menu { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$Title, [Parameter(Mandatory=$true)] [array]$Options, [bool]$ShowBackOption = $true ) Clear-Host Write-Host "`n" -NoNewline Write-Host "=" * 80 -ForegroundColor Cyan Write-Host $Title -ForegroundColor Cyan Write-Host "=" * 80 -ForegroundColor Cyan Write-Host "`n" # Display options for ($i = 0; $i -lt $Options.Count; $i++) { Write-Host " $($i + 1). $($Options[$i])" -ForegroundColor White } if ($ShowBackOption) { Write-Host "`n 0. Back / Exit" -ForegroundColor Yellow } Write-Host "`n" + ("=" * 80) -ForegroundColor Cyan Write-Host "`nSelect an option: " -NoNewline -ForegroundColor Green $selection = Read-Host try { $selectionNum = [int]$selection if ($ShowBackOption -and $selectionNum -eq 0) { return 0 } if ($selectionNum -ge 1 -and $selectionNum -le $Options.Count) { return $selectionNum } else { Write-Host "Invalid selection. Please try again." -ForegroundColor Red Start-Sleep -Seconds 1 return Show-Menu -Title $Title -Options $Options -ShowBackOption $ShowBackOption } } catch { Write-Host "Invalid input. Please enter a number." -ForegroundColor Red Start-Sleep -Seconds 1 return Show-Menu -Title $Title -Options $Options -ShowBackOption $ShowBackOption } } |