Private/Read-NSPConditionalAccessCountry.ps1

function Read-NSPConditionalAccessCountry {
    <#
    .SYNOPSIS
        Dashboard: asks for two-letter country codes, with a reference link, and shows each code's country name to confirm.
    .DESCRIPTION
        Codes are ISO 3166-1 alpha-2, the form Entra named locations use. A code .NET does not
        recognize is shown as unknown and must be confirmed. Returns the codes in upper case,
        or nothing when the operator enters none.
    #>

    [CmdletBinding()]
    [OutputType([string[]])]
    param(
        [string]$Prompt = 'Country code',
        [switch]$Multiple
    )

    Write-NSPConsoleSegment -Segment @(@(' Country codes are two letters (ISO 3166-1 alpha-2), for example DE Germany, MX Mexico, GB United Kingdom. Full list: ', 'Muted'))
    Write-NSPConsoleLine " $(Get-NSPConditionalAccessCountryReference)" -Role Accent
    while ($true) {
        $answer = Read-NSPConditionalAccessText -Prompt $Prompt
        if (-not $answer) { return }
        $codes = @($answer.ToUpperInvariant() -split '[,;\s]+' | Where-Object { $_ } | Select-Object -Unique)
        if (-not $Multiple -and $codes.Count -gt 1) { Write-NSPConsoleLine ' Enter one code.' -Role Warning; continue }
        $bad = @($codes | Where-Object { $_ -notmatch '^[A-Z]{2}$' })
        if ($bad.Count) { Write-NSPConsoleLine " Not a two-letter code: $($bad -join ', ')" -Role Warning; continue }
        $unknown = $false
        foreach ($code in $codes) {
            $name = try { ([System.Globalization.RegionInfo]::new($code)).EnglishName } catch { $null }
            if ($name) { Write-NSPConsoleSegment -Segment @(@(" $code ", 'Key'), $name) }
            else { Write-NSPConsoleSegment -Segment @(@(" $code ", 'Key'), @('not a country this computer recognizes; check the list', 'Warning')); $unknown = $true }
        }
        if (Read-NSPConfirm -Prompt $(if ($unknown) { 'Use these codes anyway?' } else { 'Are these right?' }) -DefaultYes:(-not $unknown)) { return $codes }
    }
}