-
Notifications
You must be signed in to change notification settings - Fork 0
/
Initialize-Choice.ps1
54 lines (48 loc) · 1.09 KB
/
Initialize-Choice.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
function Initialize-Choice {
[CmdletBinding()]
param (
# title of the choice
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 0
)]
[string] $Title,
# hashtable of choices and labels
[Parameter(
Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName,
Position = 1
)]
[Collections.Specialized.OrderedDictionary] $Choice
)
begin {
[int] $i = 0
}
process {
# make a list of the passed in choices
$CS = foreach ($Ch in $Choice.GetEnumerator()) {
[Management.Automation.Host.ChoiceDescription]::new(
"&$i - $($Ch.Key)",
"$($Ch.Key) - $($Ch.Value)"
)
$i++
}
# add in a "cancel" option
$CS += [Management.Automation.Host.ChoiceDescription]::new(
"&$i - Cancel",
"Choose this option to cancel"
)
}
end {
$ChoicePrompt = [Management.Automation.Host.ChoiceDescription[]] ($CS)
$Host.UI.PromptForChoice(
"Choose an option:",
$Title,
$ChoicePrompt,
$i
)
}
}