Public/Add-MaskMapping.ps1

function Add-MaskMapping {
    <#
    .SYNOPSIS
        Adds or updates an entry in the mapping file.

    .DESCRIPTION
        Adds a key -> value pair to the mapping JSON file used by Protect-SensitiveData. If the key
        already exists its value is updated (use -NoClobber to keep the existing value instead).
        The mapping file (and any missing parent folders) is created if it does not exist.
        Accepts pipeline input by property name, so objects with Key/Value properties can be piped in.

    .PARAMETER Key
        The term to find (the original, sensitive value).

    .PARAMETER Value
        The replacement (masked) value.

    .PARAMETER MappingFile
        Path to the mapping JSON file. Defaults to MaskSensitiveData_Map.json in the user's profile.

    .PARAMETER NoClobber
        Do not overwrite an existing key; warn and leave its current value unchanged.

    .PARAMETER PassThru
        Return the resulting Key/Value entry as an object.

    .EXAMPLE
        Add-MaskMapping -Key 'Contoso' -Value 'Acme'

        Adds (or updates) the Contoso -> Acme mapping in the default mapping file.

    .EXAMPLE
        [pscustomobject]@{ Key = 'john'; Value = 'alice' } | Add-MaskMapping

        Adds a mapping from a piped object.

    .LINK
        Remove-MaskMapping
    .LINK
        Get-MaskMapping
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [string]$Key,

        [Parameter(Mandatory, Position = 1, ValueFromPipelineByPropertyName)]
        [string]$Value,

        [string]$MappingFile = (Join-Path $env:USERPROFILE 'MaskSensitiveData_Map.json'),

        [switch]$NoClobber,

        [switch]$PassThru
    )

    begin {
        $map = Import-Mapping -MappingFile $MappingFile
    }

    process {
        if ($map.ContainsKey($Key)) {
            if ($NoClobber) {
                Write-Warning "Key '$Key' already exists with value '$($map[$Key])'; left unchanged (-NoClobber)."
                if ($PassThru) { [pscustomobject]@{ Key = $Key; Value = $map[$Key] } }
                return
            }
            $action = "Update mapping '$Key' -> '$Value' (was '$($map[$Key])')"
        } else {
            $action = "Add mapping '$Key' -> '$Value'"
        }

        if ($PSCmdlet.ShouldProcess($MappingFile, $action)) {
            $map[$Key] = $Value
            if ($PassThru) { [pscustomobject]@{ Key = $Key; Value = $Value } }
        }
    }

    end {
        if ($PSCmdlet.ShouldProcess($MappingFile, 'Save mapping file')) {
            Export-Mapping -Map $map -MappingFile $MappingFile
        }
    }
}