-
Notifications
You must be signed in to change notification settings - Fork 7
/
CliMenu.psm1
834 lines (732 loc) · 21.5 KB
/
CliMenu.psm1
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
function Add-MenuItem
{
<#
.Synopsis
Add a Menu Item to a menu.
.DESCRIPTION
Add a Menu Item to a menu. This cmdlet support input (Menu Items) from the pipeline.
.EXAMPLE
C:> $items = Get-MenuItem -MenuName main
C:> $items | Add-MenuItem -Menu subMenu
This will copy Menu Items from the main Menu and add them to the Menu subMenu.
.EXAMPLE
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Show-Command -Name Unlock-UserObject }
DisableConfirm = $true
}
C:> $item = New-MenuItem @newMenuItem
C:> $item | Add-MenuItem -Menu main
This will create a new Menu Item and add it to the main Menu using the pipeline.
.EXAMPLE
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Show-Command -Name Unlock-UserObject }
DisableConfirm = $true
}
C:> $item = New-MenuItem @newMenuItem
C:> Add-MenuItem -Menu main -MenuItem $item
This will create a new Menu Item and add it to the main Menu.
.NOTES
NAME: Add-MenuItem
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding()]
Param
(
[Parameter(Mandatory)]
[string]
$Menu
,
[Parameter(Mandatory, ValueFromPipeline)]
[PSCustomObject]
$MenuItem
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
Write-Verbose "getting menu"
$menuObject = Get-Menu -Name "$Menu"
if ($menuObject)
{
write-verbose "found menu"
foreach ($Item in $menuObject.MenuItems)
{
if ($Item.Name -eq $MenuItem.Name)
{
Write-Error -Message "$f - Duplicate MenuItem name detected in menu [$($menuObject.Name)]"
break
}
}
$menuIndex = $script:Menus.IndexOf($menuObject)
write-verbose "menuindex [$menuIndex]"
if ($menuIndex -ge 0)
{
$null = $script:Menus[$menuIndex].MenuItems.Add($MenuItem)
}
}
else {
Write-Verbose "no menuobject"
}
}
END
{
Write-Verbose -Message "$f - END"
}
}
$script:Menus = New-Object -TypeName System.Collections.ArrayList
$script:MenuOptions = [pscustomobject]@{
MenuFillChar = "*"
MenuFillColor = [consolecolor]::White
Heading = ""
HeadingColor = [consolecolor]::White
SubHeading = ""
SubHeadingColor = [consolecolor]::White
FooterText = ""
FooterTextColor = [consolecolor]::White
MenuItemColor = [consolecolor]::White
MenuNameColor = [consolecolor]::White
MaxWith = 80
}
function Get-Menu
{
<#
.Synopsis
Get a list of menus
.DESCRIPTION
Returns a list of menus by name, id or just the main menu
.EXAMPLE
C:> Get-Menu
Returns all menus
.EXAMPLE
C:> Get-Menu -MainMenu
Returns the Main Menu only
.EXAMPLE
C:> Get-Menu -MenuID 1
Returns the menu of the specified index
.EXAMPLE
C:> Get-Menu -Name main*
Returns all the menus which has a name that starts with main
.NOTES
NAME: Get-Menu
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding(DefaultParameterSetName='none')]
[OutputType([PSCustomObject])]
Param
(
[Parameter(ParameterSetName="MainMenu")]
[switch]
$MainMenu
,
[Parameter(ParameterSetName='ByID')]
[int]
$MenuID
,
[Parameter(ParameterSetName="ByName")]
[string]
$Name
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
if ($PSBoundParameters.ContainsKey("MainMenu"))
{
$script:Menus.Where({$_.IsMainMenu -eq $true})
}
if ($PSBoundParameters.ContainsKey("MenuID"))
{
$script:Menus[$MenuID]
}
if ($PSBoundParameters.ContainsKey("Name"))
{
$script:Menus.Where({$_.Name -like "$Name"})
}
if($PSCmdLet.ParameterSetName -eq "none")
{
$script:Menus
}
}
END
{
Write-Verbose -Message "$f - END"
}
}
function Get-MenuItem
{
<#
.Synopsis
Get a list of menu-items
.DESCRIPTION
Returns a list of menus by Menu-name, Menu-ID or the menu object
.EXAMPLE
C:> Get-MenuItem
Returns all menu-items for all menus
.EXAMPLE
C:> Get-MenuItem -MenuName MainMenu
Returns the menu-items for the menu with name MainMenu
.EXAMPLE
C:> Get-MenuItem -MenuId 1
Returns the menu-items for the menu with id 1
.EXAMPLE
C:> $Menu = Get-Menu -Name SubMenuSkype
C:> Get-MenuItem -MenuObject $Menu
Returns all the menu-items for the menu with name SubMenuSkype
.EXAMPLE
C:> Get-Menu -Name SubMenuSkype | Get-MenuItem
Returns all the menu-items for the menu with name SubMenuSkype
.NOTES
NAME: Get-MenuItem
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding(DefaultParameterSetName='none')]
[OutputType([PSCustomObject])]
Param
(
[Parameter(ParameterSetName="ByName")]
[string[]]
$MenuName
,
[Parameter(ParameterSetName="ById")]
[int]
$MenuId
,
[Parameter(ValueFromPipeline, ParameterSetName="ByObject")]
[PSCustomObject]
$MenuObject
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
if ($PSCmdlet.ParameterSetName -eq "none")
{
$script:Menus.MenuItems
}
if ($PSBoundParameters.ContainsKey("MenuName"))
{
write-verbose -message "$f - Getting by MenuName"
$script:Menus.Where({$_.Name -eq "$MenuName"}) | Select-Object -ExpandProperty MenuItems
}
if ($PSBoundParameters.Containskey("MenuId"))
{
$script:Menus[$MenuId].MenuItems
}
if ($PSCmdlet.ParameterSetName -eq "ByObject")
{
$MenuObject.MenuItems
}
}
END
{
Write-Verbose -Message "$f - END"
}
}
function Get-MenuOption
{
<#
.Synopsis
Get a list menu options
.DESCRIPTION
Returns a PSCustomObject with all menu options. This CmdLet has no parameters
.EXAMPLE
C:> Get-MenuOption
Returns all menu-items for all menus
.NOTES
NAME: Get-MenuItem
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding()]
[OutputType([PSCustomObject])]
Param
()
$script:MenuOptions
}
function New-Menu
{
<#
.Synopsis
Create a new Menu
.DESCRIPTION
You can create as many menus you like, however you may only have one main Menu. The Menu must
have a name, hence the Name parameter is Mandatory. The first Menu you create will become
the main Menu even if you do not specify the IsMainMenu switch.
.PARAMETER Name
Normally you would like to specify a name without space and Camel-case the name.
.EXAMPLE
C:> New-Menu -Name "MainMenu"
This will create a new Menu with name MainMenu. If this is the first Menu, it will be
created as a main Menu
.EXAMPLE
C:> New-Menu -Name "MainMenu" -IsMainMenu
This will create a new Menu with name MainMenu and set is as a main Menu
.EXAMPLE
C:> New-Menu -Name "sub1" -DisplayName "Sub-Menu for Skype"
This will create a new Menu with name sub1 and DisplayName Sub-Menu for Skype
.NOTES
NAME: New-Menu
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding()]
[OutputType([PSCustomObject])]
Param
(
[Parameter(Mandatory)]
[string]
$Name
,
[string]
$DisplayName
,
[switch]
$IsMainMenu
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
$newMenu = [PSCustomObject]@{
Name = "$Name"
DisplayName = "$DisplayName"
IsMainMenu = $IsMainMenu
MenuItems = New-Object -TypeName System.Collections.ArrayList
}
$currentMainMenu = Get-Menu -MainMenu
if ($PSBoundParameters.ContainsKey("IsMainMenu"))
{
if ($currentMainMenu)
{
Write-Error -Message "$f - You can only have one Main Menu. Currently [$($currentMainMenu.Name)] is your main menu"
break
}
}
if (-not $currentMainMenu)
{
$newMenu.IsMainMenu = $true
}
write-Verbose -Message "Creating menu [$Name]"
$null = $script:Menus.Add($newMenu)
$newMenu
}
END
{
}
}
function New-MenuItem
{
<#
.Synopsis
Create a new Menu-Item for a Menu.
.DESCRIPTION
Menu-Items are the action elements of the Menu. You add Menu-Items to a Menu.
.EXAMPLE
C:> New-MenuItem -Name "PasswordReset" -DisplayName "Reset a user password" -Action { Set-UserPassword }
This will create a new Menu-Item.
Since no MenuId is specified, it return the new object to the console. The switch parameter
DisableConfirm is not specified and the user will have to confirm the invokation after it
has been selected.
.EXAMPLE
C:> $menu = Get-Menu -Name sub1
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Unlock-UserObject }
DisableConfirm = $true
}
C:> $menu | New-MenuItem @newMenuItem
This will create a new Menu-Item for the menu named sub1. The Menu-object is piped into the New-MenuItem cmdlet.
It will invoke a custom cmdlet Unlock-UserObject and it will not confirm with the user before invokation.
.EXAMPLE
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Unlock-UserObject }
DisableConfirm = $true
}
C:> New-Menu -Name "sub1" -DisplayName "Sub-Menu for Skype" | New-MenuItem @newMenuItem
This will create a new Sub-Menu and add the UnlockUser Menu-Item to it using the pipeline.
It will invoke a custom cmdlet Unlock-UserObject and it will not confirm with the user before invokation.
.EXAMPLE
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Show-Command -Name Unlock-UserObject }
DisableConfirm = $true
}
C:> New-Menu -Name "sub1" -DisplayName "Sub-Menu for Skype" | New-MenuItem @newMenuItem
This will create a new Sub-Menu and add the UnlockUser Menu-Item to it using the pipeline.
It will invoke the Show-Command cmdlet which will show a windows form with the parameters of the custom
cmdlet Unlock-UserObject. It will not confirm with the user before invokation. The user may cancel the
windows form without executing the cmdlet.
.EXAMPLE
C:> $mainMenu = New-Menu -Name Main -DisplayName "Main Menu" -IsMainMenu
C:> $newMenuItem = @{
Name = "UnlockUser"
DisplayName = "Unlock a user"
Action = { Show-Command -Name Unlock-UserObject }
DisableConfirm = $true
}
C:> $item = New-MenuItem $newMenuItem
C:> $item | Add-MenuItem -Menu Main
This will create a new Sub-Menu and add the UnlockUser Menu-Item to it using the pipeline.
.NOTES
NAME: New-MenuItem
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding(DefaultParameterSetName="none")]
[OutputType([PSCustomObject])]
Param
(
[string]
$Name
,
[string]
$DisplayName
,
[string]
$Description
,
[scriptblock]
$Action
,
[switch]
$DisableConfirm
,
[Parameter(ParameterSetName="ByName")]
[string]
$MenuName
,
[Parameter(ValueFromPipeline, ParameterSetName="ByObject")]
[PSCustomObject]
$MenuObject
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
}
PROCESS
{
$menuItem = [PSCustomObject]@{
Name = "$Name"
DisplayName = "$DisplayName"
Description = "$Description"
Action = $Action
ConfirmBeforeInvoke = -not $DisableConfirm
}
if ($PSBoundParameters.ContainsKey("MenuName"))
{
$MenuObject = Get-Menu -Name $MenuName
}
else
{
#$menu = Get-Menu -MainMenu
}
foreach ($Item in $menu.MenuItems)
{
if ($Item.Name -eq "$Name")
{
Write-Error -Message "$f - Duplicate MenuItem name detected in menu [$($menu.Name)]"
break
}
}
if ($PSBoundParameters.ContainsKey("MenuObject") -or $MenuObject)
{
$menuIndex = $script:Menus.IndexOf($MenuObject)
$null = $script:Menus[$menuIndex].MenuItems.Add($menuItem)
}
#
#if ($menuIndex -eq -1)
#{
# throw "$f - Error, unable to find menu"
#}
#
$menuItem
}
END
{
Write-Verbose -Message "$f - END"
}
}
function Set-MenuOption
{
[cmdletbinding()]
Param
(
[string]
$MenuFillChar = "*"
,
[ConsoleColor]
$MenuFillColor = [consolecolor]::white
,
[string]
$Heading = "[Heading not set]"
,
[ConsoleColor]
$HeadingColor = [consolecolor]::white
,
[string]
$SubHeading = "[SubHeading not set]"
,
[ConsoleColor]
$SubHeadingColor = [consolecolor]::white
,
[string]
$FooterText
,
[ConsoleColor]
$FooterTextColor = [consolecolor]::white
,
[consolecolor]
$MenuItemColor = [consolecolor]::white
,
[consolecolor]
$MenuNameColor = [consolecolor]::white
,
[int]
$MaxWith = 80
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
foreach ($key in $PSBoundParameters.Keys)
{
Write-Verbose -Message "$f - Setting [$key] to value $($PSBoundParameters.$key)"
$script:MenuOptions.$key = $PSBoundParameters.$key
}
if ([string]::IsNullOrEmpty($script:MenuOptions.FooterText))
{
$script:MenuOptions.FooterText = "$(Get-date) - Running as $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)"
}
}
END
{
Write-Verbose -Message "$f - END"
}
}
function Show-Menu
{
<#
.Synopsis
Show a Menu.
.DESCRIPTION
If executed without parameters, it will build and show the main Menu. If the MenuID parameter is
specified, it will show the menu with that ID. You may also use the cmdlet to invoke a specific
Menu-Item on a specific menu.
.EXAMPLE
C:> Show-Menu
This will show the main Menu if defined.
.EXAMPLE
C:> Show-Menu -MenuId 1
This will show the menu at index 1. Use Get-Menu to find the index (zero-based array)
.EXAMPLE
C:> Show-Menu -InvokeItem 2 -MenuId 0
This will invoke the Menu-Item at index 2 on the form at index 0. If the Menu-Item requires
confirmation before invoking it, the user will be prompted before invokation.
.EXAMPLE
C:> Show-Menu -InvokeItem 2 -MenuId 0 -force
This will invoke the Menu-Item at index 2 on the form at index 0. If the Menu-Item requires
confirmation before invoking it, the user will not be prompted before invokation since the
force flag has been specified.
.NOTES
NAME: Show-Menu
AUTHOR: Tore Groneng [email protected] @toregroneng [email protected]
LASTEDIT: Aug 2016
KEYWORDS: General scripting Controller Menu
#>
[cmdletbinding()]
Param
(
[int]
$InvokeItem
,
[switch]
$Force
,
[string]
$MenuName
)
BEGIN
{
$f = $MyInvocation.InvocationName
Write-Verbose -Message "$f - START"
$mainMenu = Get-Menu -MainMenu
if (-not $mainMenu)
{
Write-Warning -Message "Please add a menu first using the New-Menu cmdlet"
break
}
}
PROCESS
{
if ($PSBoundParameters.ContainsKey("InvokeItem"))
{
$menuSelected = (Get-Menu -Name $MenuName).MenuItems[$InvokeItem] #$script:Menus[$MenuID].MenuItems[$InvokeItem]
if($menuSelected)
{
if ($menuSelected.ConfirmBeforeInvoke)
{
if (-not $Force)
{
$Continue = Read-Host -Prompt "Are you sure you want to execute [$($menuSelected.Name)] Y/N?"
If ($Continue -ne "y")
{
Write-Host -Object "Execution aborted" -ForegroundColor DarkYellow
break
}
}
}
if ($menuSelected.Action)
{
Write-Host -Object "Invoking [$($menuSelected.Name)]" -ForegroundColor DarkYellow
$menuSelected.Action.Invoke()
Write-Host -Object "Invoke DONE!" -ForegroundColor DarkYellow
break
}
}
}
}
END
{
$menuLines = New-Object -TypeName System.Collections.ArrayList
$maxWith = $script:MenuOptions.MaxWith
function Get-MenuLine
{
Param
(
[string]
$Text
,
[consolecolor]
$Color = [System.ConsoleColor]::White
,
[bool]
$IsMenuItem = $false
)
if ($IsMenuItem)
{
$textLine = " " + "$Text"
$textLine += " " * (($maxWith - 1) - $textLine.length - 1)
}
else
{
$maxWith = $script:MenuOptions.MaxWith
$textLength = $Text.Length
$textBlanks = (($maxWith - 2) - $textLength) / 2
$textLine = " " * $textBlanks + $Text
$textLine += " " * (($maxWith - 1) - $textLine.Length - 1)
}
[pscustomobject]@{
Text = "$textLine"
Color = $color
}
}
if ($PSBoundParameters.ContainsKey("MenuName"))
{
$menu = Get-Menu -Name $MenuName
}
else
{
$menu = Get-Menu -MainMenu
}
if (-not $menu)
{
Write-Error -Exception "$f - Could not find menu"
}
$menuIndex = $script:Menus.IndexOf($menu)
$menuFrame = $script:MenuOptions.MenuFillChar * ($maxWith - 2)
$null = $menuLines.Add((Get-MenuLine -Text $menuFrame -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menu.DisplayName -Color $script:MenuOptions.MenuNameColor))
$menuEmptyLine = " " * ($maxWith - 2)
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $script:MenuOptions.Heading -color $script:MenuOptions.HeadingColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $script:MenuOptions.SubHeading -color $script:MenuOptions.SubHeadingColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuFrame -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
foreach ($item in $menu.MenuItems)
{
$menuColor = $script:MenuOptions.MenuItemColor
$menuItemIndex = $script:menus[$menuIndex].MenuItems.IndexOf($item)
$null = $menuLines.Add((Get-MenuLine -Text "$menuItemIndex. $($item.DisplayName)" -IsMenuItem $true -Color $menuColor))
}
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $script:MenuOptions.FooterText -color $script:MenuOptions.FooterTextColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuEmptyLine -color $script:MenuOptions.MenuFillColor))
$null = $menuLines.Add((Get-MenuLine -Text $menuFrame -color $script:MenuOptions.MenuFillColor))
foreach ($line in $menuLines)
{
Write-Host -Object $script:MenuOptions.MenuFillChar -ForegroundColor $script:MenuOptions.MenuFillColor -NoNewline
Write-Host -Object $line.Text -ForegroundColor $line.Color -NoNewline
Write-Host -Object $script:MenuOptions.MenuFillChar -ForegroundColor $script:MenuOptions.MenuFillColor
}
$userSelection = (Read-Host -Prompt "Please enter number to execute action")
try
{
$actionItemSelectionIndex = [int]($userSelection)
}
catch
{
Write-Error -Message $_.Exception.Message
Write-Host -Object "Menuitem not found [$userSelection]" -ForegroundColor DarkYellow
break
}
$menuSelected = $menu.MenuItems[$actionItemSelectionIndex]
if ($menuSelected)
{
if ($menuSelected.ConfirmBeforeInvoke)
{
$Continue = Read-Host -Prompt "Are you sure you want to execute [$($menuSelected.Name)] Y/N?"
If ($Continue -ne "y")
{
Write-Host -Object "Execution aborted" -ForegroundColor DarkYellow
break
}
}
if ($menuSelected.Action)
{
Write-Host -Object "Invoking [$($menuSelected.Name)]" -ForegroundColor DarkYellow
$menuSelected.Action.Invoke()
Write-Host -Object "Invoke DONE!" -ForegroundColor DarkYellow
}
}
else
{
Write-Host -Object "Menuitem not found [$userSelection]" -ForegroundColor DarkYellow
}
Write-Verbose -Message "$f- END"
}
}