Private/Get-SqlSpnVisualAccountSelection.ps1

# =============================================================================
# Script : Get-SqlSpnVisualAccountSelection.ps1
# Author : Keith Ramsey
# =============================================================================
# Change Log
# -----------------------------------------------------------------------------
# 2026-05-09 Keith Ramsey Phase 2 release polish - DR-202 standard header applied.
# =============================================================================
function Get-SqlSpnVisualAccountSelection {
    <#
    .SYNOPSIS
        Presents a list of accounts via Out-GridView (or fallback) and returns the chosen one.
    .DESCRIPTION
        Tries Out-GridView (Windows PowerShell 5.1) first, falls back to Out-ConsoleGridView
        (PS 7 with Microsoft.PowerShell.ConsoleGuiTools), and finally to a numbered Read-Host
        prompt when neither is available. Returns $null on cancellation.
    #>

    [CmdletBinding()]
    param([System.Collections.IEnumerable]$AccountList)

    if (Get-Command -Name Out-GridView -ErrorAction SilentlyContinue) {
        return $AccountList | Out-GridView -Title 'Which service identity are we managing?' -OutputMode Single
    }
    if (Get-Command -Name Out-ConsoleGridView -ErrorAction SilentlyContinue) {
        return $AccountList | Out-ConsoleGridView -Title 'Which service identity are we managing?' -OutputMode Single
    }

    $i = 0
    $indexed = $AccountList | ForEach-Object { [PSCustomObject]@{ Index = $i++; Item = $_ } }
    $indexed | Format-Table -AutoSize | Out-Host
    $choice = Read-Host 'Select index (blank to cancel)'
    if ([string]::IsNullOrWhiteSpace($choice)) { return $null }
    $entry = $indexed | Where-Object { $_.Index -eq [int]$choice }
    return $entry.Item
}