-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-MenuSelection.ps1
71 lines (63 loc) · 2.52 KB
/
Get-MenuSelection.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
function Get-MenuSelection {
<#
.SYNOPSIS
This function takes in an array or object and outputs it as a menu
and then returns the selection as the result
.DESCRIPTION
This function takes in an array or object and outputs it as a menu
and then returns the selection as the result
.NOTES
Author: LegoBenny aka Forrest (https://github.com/legoBenny)
.EXAMPLE
gci "C:\scripts\VirtScripts\includes" | Get-MenuSelection
Get-MenuSelection -MenuItems $(get-datacenter 123456-DC | get-cluster)
Get-MenuSelection -MenuItems $(get-process | select -expandProperty ProcessName)
Get-MenuSelection -MenuItems @("Apple","Bananna","Cherry") -MenuTitle "My Wonderful Fruit Menu" -MenuMessage "Please enter the number of the fruit you want"
.EXAMPLE
get-vm | Get-MenuSelection
Please select from the following options:
1. vm-web.abcdef.com
2. vm-db.abcdef.com
3. vm-web2.abcdef.com
4. vmapp2.abcdef.com
5. vmdb1.abcdef.com
6. vmdb2.abcdef.com
7. s2fqaapp.abcdef.com
Selection Number: 4
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
vmdb1.abcdef.com PoweredOff 4 16.000
#>
[cmdletbinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][object[]]$MenuItems,
[Parameter()][string]$MenuTitle = "Please select from the following options",
[Parameter()][string]$MenuMessage = "Selection Number"
)
begin{
$items = @()
Write-Host "`r`n$($menuTitle):"
}
process{
$items += $MenuItems # get the items from the pipe and put them in an array
foreach ($item in $MenuItems) {
write-host " $($items.indexof($item)+1). $item"
}
}
end{
try{$selectNum = [int](read-host "`r`n$MenuMessage")}
catch{
Write-Host "Invalid Selection: Selection must be an integer." -fore red;
Get-MenuSelection -MenuItems $items -MenuTitle $menuTitle -MenuMessage $MenuMessage
break
}
if (!($selectNum -ge 1 -and $selectNum -le ($items.count))) {
write-host "Invalid Selection: Selection must be between 1 and $($items.count)." -fore red
Get-MenuSelection -MenuItems $items -MenuTitle $menuTitle -MenuMessage $MenuMessage
break
}else{
$result = $items[$($selectNum - 1)]
return $result
}
}
}