Repository navigation
Expand file tree
/
Copy pathInteractivePowershellMenu.ps1
More file actions
175 lines (149 loc) · 5.07 KB
/
Copy pathInteractivePowershellMenu.ps1
File metadata and controls
175 lines (149 loc) · 5.07 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<#
.Description
Created by Bernhard Sumser (2020-11-28)
This powershell script displays an interactive menu by using the passed parameters.
Please beware the not existing licence. If you are a company the usage is forbidden.
GITHUB Project: https://github.com/PlanBernhard/PowershellMenuScreen
#>
[CmdletBinding()]
param (
# Options to show
[Parameter(Mandatory = $true)]
[string[]]
$OptionsToSelect,
# Choose between Single or Multiselect
[Parameter(Position = $true)]
[bool]
$MultiSelect
)
Clear-Host
<#
.DESCRIPTION
This function will show a interactive menu with the passed arguments as menu options.
.INPUTS
$MenuDescription - String that is shown in the menu title text.
$MenuOptions - String array with the options to chose.
.OUTPUTS
Outputs a integer number. The first selection starts with 0.
#>
function ShowMultiSelectMenu() {
[CmdletBinding()]
param (
# Text to display in menu
[Parameter()]
[string]
$MenuDescription,
# Options to select
[Parameter(Mandatory = $true)]
[string[]]
$MenuOptions
)
$showMenu = $true
$menuSelection = 0
$somethingSelected = $true
if (!$MenuOptions) {
throw "MenuOptions cannot be Null!"
Read-Host
}
$menuObjectList = New-Object Collections.Generic.List[object]
foreach ($option in $MenuOptions) {
$menuObjectList.Add(@{
option = $option
state = $false
})
}
do {
Clear-Host
if ($somethingSelected -eq $false) {
Write-Host -BackgroundColor Red -ForegroundColor White "You need to select at least one option!"
}
Write-Host "-------------------------------------------------"
Write-Host "| $MenuDescription "
Write-Host "| ↑ and ↓ arrow key to chose |"
Write-Host "| [SPACE] to select the highlighted choice |"
Write-host "| [ENTER] to accept your choices |"
Write-Host "-------------------------------------------------"
foreach ($option in $menuObjectList) {
# When selected but not chosen
if ($menuObjectList.IndexOf($option) -eq $menuSelection -and $option.state -eq $false) {
Write-Host -BackgroundColor Yellow -ForegroundColor Black "[ ]" $option.option
}
# When selected and chosen
elseif ($menuObjectList.IndexOf($option) -eq $menuSelection -and $option.state -eq $true) {
Write-Host -BackgroundColor Yellow -ForegroundColor Black "[*]" $option.option
}
# When not selected and chosen
elseif ($menuObjectList.IndexOf($option) -ne $menuSelection -and $option.state -eq $true) {
Write-Host "[*]" $option.option
}
# When not selected and not chosen
else {
Write-Host "[ ]" $option.option
}
}
$keyInput = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown").virtualkeycode
Switch ($keyInput) {
# ENTER key
13 {
$somethingSelected = $false
foreach ($item in $menuObjectList) {
if ($item.state -eq $true) {
$somethingSelected = $true
}
}
if ($somethingSelected) {
$showMenu = $false
Clear-Host
break
}
}
# SPACE key
32 {
if ($menuObjectList[$menuSelection].state -eq $false) {
$menuObjectList[$menuSelection].state = $true
}
else {
$menuObjectList[$menuSelection].state = $false
}
Clear-Host
break
}
# top arrow key
38 {
if ($menuSelection -gt 0) {
$menuSelection -= 1
}
Clear-Host
break
}
# down arrow key
40 {
if ($menuSelection -lt $MenuOptions.Length - 1) {
$menuSelection += 1
}
Clear-Host
break
}
Default {
Clear-Host
}
}
}
while ($showMenu)
$arrayToReturn = @()
foreach ($selection in $menuObjectList) {
if ($selection.state -eq $true) {
$arrayToReturn += $selection.option
}
}
return $arrayToReturn
}
if($MultiSelect -eq $true){
$selectionToReturn = ShowMultiSelectMenu -MenuDescription "What do you want to do?" -MenuOptions $OptionsToSelect
}
else{
$selectionToReturn = ShowMultiSelectMenu -MenuDescription "What do you want to do?" -MenuOptions $OptionsToSelect
}
Write-Host "Selected option(s): "
Write-Host $selectionToReturn
return $selectionToReturn