Private/New-PWSHPUKMGT-Puk.ps1

function New-PWSHPUKMGT-Puk {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Pure in-memory computation (generates and returns a candidate PUK string); changes no external or persistent state.')]
    [CmdletBinding()]
    [OutputType([string])]
    param(
        [ValidateRange(1, [int]::MaxValue)]
        [int]$Length,

        [psobject]$Config
    )

    if (-not $Config) {
        $Config = Get-PukModuleConfig
    }

    if (-not $Length) {
        $Length = $Config.puk.defaultLength
    }

    if ($Length -lt $Config.puk.minLength -or $Length -gt $Config.puk.maxLength) {
        throw "Requested PUK length $Length is outside the configured range ($($Config.puk.minLength)-$($Config.puk.maxLength))."
    }

    $maxAttempts = $Config.puk.maxGenerationAttempts
    for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
        $candidate = New-PukCngRandomValue -Length $Length
        if (-not (Test-PukWeakness -Code $candidate -WeaknessRules $Config.puk.weaknessRules)) {
            return $candidate
        }
    }

    throw "Unable to generate a PUK compliant with the configured weakness rules after $maxAttempts attempts. Check 'puk.weaknessRules' for an overly strict configuration."
}