Functions/Add-BuiltInMenuItem.ps1
|
# --------------------------------------------------------------------------- # Internal registry of built-in items # --------------------------------------------------------------------------- $script:BuiltInItems = @{ # Go to parent menu 'GoToParent' = @{ Name = 'BuiltIn_GoToParent' DisplayName = 'Go Back' Action = { [PSCustomObject]@{ menu = "back" } } DisableConfirm = $true } # Go to top level menu 'GoToMainMenu' = @{ Name = 'BuiltIn_GoToMainMenu' DisplayName = 'Go to Main Menu' Action = { [PSCustomObject]@{ menu = "main" } } DisableConfirm = $true } # Exits all menus 'Exit' = @{ Name = 'BuiltIn_Exit' DisplayName = 'Exit' Action = { [PSCustomObject]@{ menu = "exit" } } DisableConfirm = $true } # Prints the current date and time 'Timestamp' = @{ Name = 'BuiltIn_Timestamp' DisplayName = 'Show current date/time' Action = { Write-Output "Current time: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" } DisableConfirm = $true } # Prints the current PowerShell version 'PSVersion' = @{ Name = 'BuiltIn_PSVersion' DisplayName = 'Show PowerShell version' Action = { Write-Output "PowerShell version: $($PSVersionTable.PSVersion)" } DisableConfirm = $true } } # --------------------------------------------------------------------------- # Public helper function # --------------------------------------------------------------------------- function Add-BuiltInMenuItem { <# .SYNOPSIS Adds a pre-defined built-in menu item to a named cliMenu menu. .PARAMETER MenuName The Name of the menu to add the item to (e.g. 'Main'). .PARAMETER Item The key of the built-in item to add. Valid values: GoToParent, GoToMainMenu, Exit, Timestamp, PSVersion .EXAMPLE Add-BuiltInMenuItem -MenuName 'Main' -Item 'GoToParent' .EXAMPLE # Add multiple items 'GoToParent','GoToMainMenu','Exit','Timestamp','PSVersion' | ForEach-Object { Add-BuiltInMenuItem -MenuName 'Main' -Item $_ } #> [CmdletBinding()] param( [Parameter(Mandatory)] [string] $MenuName, [Parameter(Mandatory)] [ValidateSet('GoToParent', 'GoToMainMenu', 'Exit', 'Timestamp', 'PSVersion')] [string] $Item ) if (-not $script:BuiltInItems.ContainsKey($Item)) { Write-Error "Unknown built-in item: '$Item'" return } $itemDef = $script:BuiltInItems[$Item] $menuItem = New-MenuItem ` -Name $itemDef.Name ` -DisplayName $itemDef.DisplayName ` -Action $itemDef.Action ` -DisableConfirm:$itemDef.DisableConfirm $menuItem | Add-MenuItem -Menu $MenuName Write-Verbose "Added built-in item '$Item' to menu '$MenuName'" } |