Private/Select-EFMenuControlId.ps1
|
function Select-EFMenuControlId { [CmdletBinding()] param( [Parameter(Mandatory)] [object[]]$Steps, [switch]$NoColor, [ValidateRange(20, 240)] [int]$Width = 80 ) $candidates = @($Steps | Where-Object Action -eq 'Automatic') if ($candidates.Count -eq 0) { return } Write-EFMenuLine -Text '' -NoColor:$NoColor -Width $Width Write-EFMenuLine -Text 'Select automatic controls' -Color Cyan -NoColor:$NoColor -Width $Width for ($index = 0; $index -lt $candidates.Count; $index++) { $candidate = $candidates[$index] Write-EFMenuLine -Text ("{0}. {1} - {2}{3}" -f ($index + 1), $candidate.ControlId, $candidate.Title, ` $(if ($candidate.RequiresReboot) { ' [restart may be required]' } else { '' })) ` -NoColor:$NoColor -Width $Width -Indent 2 } while ($true) { $selection = Read-EFMenuInput -Prompt 'Enter numbers separated by commas, A for all, or B to cancel' if ($null -eq $selection -or $selection.Trim() -match '^(?i:b|back|q|quit)$') { return } if ($selection.Trim() -match '^(?i:a|all)$') { return @($candidates.ControlId) } $selectedIndices = [Collections.Generic.List[int]]::new() $isValid = $true foreach ($token in @($selection -split '[,\s]+' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })) { $parsed = 0 if (-not [int]::TryParse($token, [ref]$parsed) -or $parsed -lt 1 -or $parsed -gt $candidates.Count) { $isValid = $false break } if (-not $selectedIndices.Contains($parsed - 1)) { $selectedIndices.Add($parsed - 1) } } if ($isValid -and $selectedIndices.Count -gt 0) { return @($selectedIndices | ForEach-Object { [string]$candidates[$_].ControlId }) } Write-EFMenuLine -Text '[INVALID] Choose listed numbers, A, or B.' -Color Yellow -NoColor:$NoColor -Width $Width } } |