Private/Read-NSPEntraAddressPolicy.ps1

function Read-NSPEntraAddressPolicy {
    [CmdletBinding()]
    param()
    $format = 'ReplaceSuffix'
    $primaryAction = 'Block'
    $policy = @{ChangeSamAccountName=$false;CreateMailIfEmpty=$true;UpdateUPN=$true;UpdateMail=$true;
        UpdatePrimarySmtp=$true;PreserveOldAddress=$true;ConvertAliasDomain=$false;
        RemoveAliasDomain=[string[]]@();RemoveAddressType=[string[]]@()}
    while ($true) {
        $choices = @([pscustomobject]@{Label="Preview with these settings (name format: $format)";Value='preview'},
            [pscustomobject]@{Label='Change name format';Value='format'},
            [pscustomobject]@{Label="Cloud/AD primary mismatch: $primaryAction";Value='primary-conflict'})
        $labels=@{UpdateUPN='Update sign-in UPN';UpdateMail='Update mail';UpdatePrimarySmtp='Update primary SMTP';CreateMailIfEmpty='Create mail when empty';ChangeSamAccountName='Rename sAMAccountName';PreserveOldAddress='Preserve old aliases';ConvertAliasDomain='Also convert alias suffixes'}
        foreach ($name in @('UpdateUPN','UpdateMail','UpdatePrimarySmtp','CreateMailIfEmpty','ChangeSamAccountName','PreserveOldAddress','ConvertAliasDomain')) {
            $choices += [pscustomobject]@{Label=("{0}: {1}" -f $labels[$name],$policy[$name]);Value=$name}
        }
        $choices += @([pscustomobject]@{Label=('Alias domains to remove: '+($policy.RemoveAliasDomain -join ', '));Value='remove-domain'},
            [pscustomobject]@{Label=('Legacy types to remove: '+($policy.RemoveAddressType -join ', '));Value='remove-type'})
        $answer = Read-NSPMenu -Prompt 'Address policy - existing AD proxies are preserved; matched cloud SMTP/SIP/X500 are reviewed as additions.' -Choices $choices -CancelLabel 'Back'
        if ($null -eq $answer) { return $null }
        if ($answer -eq 'preview') { return @{Format=$format;Policy=$policy;CloudPrimaryConflictAction=$primaryAction} }
        if ($answer -eq 'format') {
            $pick = Read-NSPMenu -Prompt 'Name format' -Choices @(@('ReplaceSuffix','SamAccountName','FLast','First.Last','LastF') | ForEach-Object { [pscustomobject]@{Label=$_;Value=$_} }) -CancelLabel 'Back'
            if ($pick) { $format = [string]$pick }
        } elseif ($answer -eq 'primary-conflict') {
            $pick = Read-NSPMenu -Prompt 'When the cloud primary differs' -Choices @(
                [pscustomobject]@{Label='Block and review';Value='Block'},
                [pscustomobject]@{Label='Use the reviewed AD primary; retain the cloud address as an alias unless explicitly removed';Value='UsePlannedAD'}
            ) -CancelLabel 'Back'
            if ($pick) { $primaryAction = [string]$pick }
        } elseif ($answer -eq 'remove-domain') {
            $value = Read-NSPNonEmpty -Prompt 'Alias domains to remove, comma separated; enter - to clear'
            if ($value -eq '-') { $policy.RemoveAliasDomain = [string[]]@() }
            else { $policy.RemoveAliasDomain = [string[]]@($value.Split(',') | ForEach-Object {$_.Trim()} | Where-Object {$_}) }
        } elseif ($answer -eq 'remove-type') {
            $pick = Read-NSPMenu -Prompt 'Explicit legacy-address removal' -Choices @(@('Preserve all','X400','X500','X400 and X500') | ForEach-Object { [pscustomobject]@{Label=$_;Value=$_} }) -CancelLabel 'Back'
            if ($pick) {
                $policy.RemoveAddressType = [string[]]@(switch ($pick) {'X400' {'X400'} 'X500' {'X500'} 'X400 and X500' {'X400';'X500'}})
            }
        } elseif ($policy.ContainsKey([string]$answer)) { $policy[[string]$answer] = -not $policy[[string]$answer] }
        else { throw 'Unrecognized address policy menu choice.' }
    }
}