Private/Storage/FlatFile/Set-PukFlatFileRecord.ps1

function Set-PukFlatFileRecord {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory)]
        [string]$Path,

        [Parameter(Mandatory)]
        [System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,

        [Parameter(Mandatory)]
        [psobject]$Record,

        [switch]$Force
    )

    $store = Get-PukFlatFileStore -Path $Path -Certificate $Certificate

    $records = @($store.records)
    $existingIndex = -1
    for ($i = 0; $i -lt $records.Count; $i++) {
        if ($records[$i].DistinguishedName -eq $Record.DistinguishedName) {
            $existingIndex = $i
            break
        }
    }

    if ($existingIndex -ge 0 -and -not $Force) {
        throw "A record for '$($Record.DistinguishedName)' already exists in '$Path'. Use -Force to overwrite."
    }

    if (-not $PSCmdlet.ShouldProcess($Record.DistinguishedName, 'Set PWSHPUKMGT flat-file record')) {
        return
    }

    if ($existingIndex -ge 0) {
        $records[$existingIndex] = $Record
    }
    else {
        $records += $Record
    }

    $store.records = $records
    Set-PukFlatFileStore -Path $Path -Certificate $Certificate -Store $store
}