-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRead-PromptForChoice.ps1
117 lines (114 loc) · 4.27 KB
/
Read-PromptForChoice.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
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
Function Read-PromptForChoice
{
[cmdletbinding(DefaultParameterSetName = 'StringChoices')]
Param(
[string]$Message
,
[Parameter(Mandatory, ParameterSetName = 'StringChoices')]
[ValidateNotNullOrEmpty()]
[alias('StringChoices')]
[String[]]$Choices
,
[Parameter(Mandatory, ParameterSetName = 'ObjectChoices')]
[ValidateNotNullOrEmpty()]
[alias('ObjectChoices')]
[psobject[]]$ChoiceObjects
,
[int]$DefaultChoice = -1
#[int[]]$DefaultChoices = @(0)
,
[string]$Title = [string]::Empty
,
[Parameter(ParameterSetName = 'StringChoices')]
[switch]$Numbered
)
#Build Choice Objects
switch ($PSCmdlet.ParameterSetName)
{
'StringChoices'
#Create the Choice Objects
{
if ($Numbered)
{
$choiceCount = 0
$ChoiceObjects = @(
foreach ($choice in $Choices)
{
$choiceCount++
[PSCustomObject]@{
Enumerator = $choiceCount
Choice = $choice
}
}
)
}
else
{
[char[]]$choiceEnumerators = @()
$ChoiceObjects = @(
foreach ($choice in $Choices)
{
$Enumerator = $null
foreach ($char in $choice.ToCharArray())
{
if ($char -notin $choiceEnumerators -and $char -match '[a-zA-Z]' )
{
$Enumerator = $char
$choiceEnumerators += $Enumerator
break
}
}
if ($null -eq $Enumerator)
{
$EnumeratorError = New-ErrorRecord -Exception System.Management.Automation.RuntimeException -ErrorId 0 -ErrorCategory InvalidData -TargetObject $choice -Message 'Unable to determine an enumerator'
$PSCmdlet.ThrowTerminatingError($EnumeratorError)
}
else
{
[PSCustomObject]@{
Enumerator = $Enumerator
Choice = $choice
}
}
}
)
}
}
'ObjectChoices'
#Validate the Choice Objects using the first object as a representative
{
if ($null -eq $ChoiceObjects[0].Enumerator -or $null -eq $ChoiceObjects[0].Choice)
{
$ChoiceObjectError = New-ErrorRecord -Exception System.Management.Automation.RuntimeException -ErrorId 1 -ErrorCategory InvalidData -TargetObject $ChoiceObjects[0] -Message 'Choice Object(s) do not include the required enumerator and/or choice properties'
$PSCmdlet.ThrowTerminatingError($ChoiceObjectError)
}
}
}#Switch
[Management.Automation.Host.ChoiceDescription[]]$PossibleChoices = @(
$ChoiceObjects | ForEach-Object {
$Enumerator = $_.Enumerator
$Choice = $_.Choice
$Description = if (-not [string]::IsNullOrWhiteSpace($_.Description)) {$_.Description} else {$_.Choice}
$ChoiceWithEnumerator = $(
if ($Numbered)
{
"&$Enumerator $($Choice)"
}
else
{
$index = $choice.IndexOf($Enumerator)
if ($index -eq -1)
{
"&$Enumerator $($Choice)"
}
else
{
$choice.insert($index, '&')
}
}
)
New-Object System.Management.Automation.Host.ChoiceDescription $ChoiceWithEnumerator, $Description
}
)
$Host.UI.PromptForChoice($Title, $Message, $PossibleChoices, $DefaultChoice)
}