|
| 1 | +# powershell cli menu helpers |
| 2 | + |
| 3 | +[[_TOC_]] |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +PowerShell CLI Menu Helpers is a module which offers few helpers functions to: |
| 8 | + |
| 9 | +- Render a menu inside a PowerShell CLI with options (exit and back) |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- Select items from a list witch options (cancel, filter, multiselect, etc.) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +- Select items from a table witch options (cancel, filter, multiselect, etc.) |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +### From PowerShell Gallery |
| 24 | + |
| 25 | +```powershell |
| 26 | +Install-Module -Name 'powershell-cli-menu-helpers' |
| 27 | +``` |
| 28 | + |
| 29 | +### From Source |
| 30 | + |
| 31 | +- Obtain the source |
| 32 | + |
| 33 | +``` |
| 34 | +git clone https://github.com/netcloudag/powershell-cli-menu-helpers |
| 35 | +``` |
| 36 | + |
| 37 | +- Navigate to the source directory |
| 38 | + |
| 39 | +``` |
| 40 | +cd path\to\powershell-cli-menu-helpers |
| 41 | +``` |
| 42 | + |
| 43 | +- Test |
| 44 | + |
| 45 | +``` |
| 46 | +.\test.ps1 |
| 47 | +``` |
| 48 | + |
| 49 | +- Build |
| 50 | + |
| 51 | +``` |
| 52 | +.\build.ps1 |
| 53 | +``` |
| 54 | + |
| 55 | +- Import the module |
| 56 | + |
| 57 | +``` |
| 58 | +Import-Module -Name '.\build\powershell-cli-menu-helpers' |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +### Menu rendering |
| 64 | + |
| 65 | +Use the function `Select-MenuEntryFromList` to render a menu inside the cli. \ |
| 66 | +The function provides an `ExitOption` and a `BackOption` option. \ |
| 67 | +This will allow to perform the needed action based on the returned value. \ |
| 68 | +Take also a look at `Get-Help -Name Select-MenuEntryFromList -Full` for furhter details. |
| 69 | + |
| 70 | +```powershell |
| 71 | +# Render a menu with back and exit option |
| 72 | +# Set the menu title and the menu entries |
| 73 | +[System.String[]]$MenuEntries = @('Menu 1', 'Menu 2', 'Menu 3') |
| 74 | +[System.String]$MenuTitle = "=============== Test Menu ===============" |
| 75 | +
|
| 76 | +# Ask user for the menu entry |
| 77 | +[PSCustomObject]$SelectedMenuEntry = Select-MenuEntryFromList -MenuTitle $MenuTitle -MenuEntries $MenuEntries -ExitOption -BackOption |
| 78 | +
|
| 79 | +# Do the action based on the selection |
| 80 | +switch ($SelectedMenuEntry.State) |
| 81 | +{ |
| 82 | + '%OK%' |
| 83 | + { |
| 84 | + Write-Host -Object "Selected menu entry: $($SelectedMenuEntry.MenuEntry)" |
| 85 | + } |
| 86 | +
|
| 87 | + '%BACK%' |
| 88 | + { |
| 89 | + Write-Host -Object "Back option selected" |
| 90 | + } |
| 91 | +
|
| 92 | + '%EXIT%' |
| 93 | + { |
| 94 | + Write-Host -Object "Exit option selected" |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### List rendering |
| 100 | + |
| 101 | +Use the function `Select-ItemFromList` to render a list inside the cli. \ |
| 102 | +The function provides various options like `MultiselectOption` or `FilterOption`. \ |
| 103 | +Take also a look at `Get-Help -Name Select-ItemFromList -Full` for furhter details. |
| 104 | + |
| 105 | +```powershell |
| 106 | +# Select multiple processes to get furhter detailes later |
| 107 | +$Processes = Select-ItemFromList -ListTitle "Select process, press 'd' if done" -Items (Get-Process | Select-Object -First 10) -PropertyName 'ProcessName' -MultiselectOption -CancelOption -FilterOption |
| 108 | +if ($Processes.State -eq '%OK%') |
| 109 | +{ |
| 110 | + foreach ($Process in $Processes.Items) |
| 111 | + { |
| 112 | + $Process | Select-Object -Property Id, ProcessName |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Table rendering |
| 118 | + |
| 119 | +Use the function `Select-ItemFromTable` to render a table inside the cli. \ |
| 120 | +The function provides various options like `MultiselectOption` or `FilterOption`. \ |
| 121 | +Take also a look at `Get-Help -Name Select-ItemFromTable -Full` for furhter details. |
| 122 | + |
| 123 | +```powershell |
| 124 | +# Select multiple processes to get furhter detailes later |
| 125 | +$Processes = Select-ItemFromTable -TableTitle "Select process, press 'd' if done" -Items (Get-Process | Select-Object -First 10) -PropertyName 'Id', 'Handles', 'ProcessName' -MultiselectOption -CancelOption -FilterOption |
| 126 | +if ($Processes.State -eq '%OK%') |
| 127 | +{ |
| 128 | + foreach ($Process in $Processes.Items) |
| 129 | + { |
| 130 | + $Process |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## Example |
| 136 | + |
| 137 | +An example powershell cli menu is provided under `.\example`. \ |
| 138 | +It shows how to combine the functions to display a powrshell cli menu and perfome actions. \ |
| 139 | + |
| 140 | +```powershell |
| 141 | +cd .\example\ |
| 142 | +.\menu_script.ps1 |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +## Contributing |
| 148 | + |
| 149 | +Contributions are welcome, please refere to [CONTRIBUTING.md](https://github.com/netcloudag/powershell-cli-menu-helpers/CONTRIBUTING.md) for details. |
| 150 | + |
| 151 | +## Creating a release |
| 152 | + |
| 153 | +### Requirements |
| 154 | + |
| 155 | +```powershell |
| 156 | +Install-Module -Name 'Pester' |
| 157 | +Install-Module -Name 'PSScriptAnalyzer' |
| 158 | +``` |
| 159 | + |
| 160 | +### Steps |
| 161 | + |
| 162 | +- Add / modify the test cases inside the folder `.\tests` |
| 163 | +- Run the tests (`.\test.ps1`) |
| 164 | +- Update the [CHANGELOG.md](https://github.com/netcloudag/powershell-cli-menu-helpers/CHANGELOG.md) |
| 165 | +- Update the `ModuleVersion` inside the [config.json](https://github.com/netcloudag/powershell-cli-menu-helpers/config.json) |
| 166 | +- Add release tag on github |
| 167 | +- Run the script `.\build.ps1` to create a new build |
| 168 | +- Run the scirpt `.\publish.ps1` to publish the build to PowerShell Gallery |
| 169 | + |
| 170 | +## Authors |
| 171 | + |
| 172 | +- **Philipp Knoll** - _Initial idea / Initial work_ |
| 173 | +- **Patrick Grazioli** - _Implementation_ |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +This project is licensed under MIT with Copyright (c) 2020 Netcloud AG. \ |
| 178 | +See the [LICENSE.md](https://github.com/netcloudag/powershell-cli-menu-helpers/LICENSE.md) file for details. |
0 commit comments