Public/Remove-LprMatchListEntry.ps1

function Remove-LprMatchListEntry {
    <#
    .SYNOPSIS
        Removes one or more entries from an LPR Match List
    .DESCRIPTION
        Removes one or more entries from an LPR Match List by matching the Registration Number value
    .EXAMPLE
        PS C:\> Remove-LprMatchListEntry -Name Staff -RegistrationNumber 'ABC123', 'XYZ321'
        Removes two entries, if present, from the LPR Match List named 'Staff'
    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
    param(
        # Specifies the name of the LPR Match List
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        # Specifies the Registration Number(s) to be removed
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string[]]
        $RegistrationNumber
    )

    begin {
        $ms = Get-ManagementServer
        if ($null -eq $ms.LprMatchListFolder) {
            throw "Milestone XProtect LPR Plugin is not installed or was not loaded in the current session. Please ensure the plugin is installed. If LPR cannot be managed from Management Client on this system, it cannot be managed from PowerShell either."
        }
    }

    process {
        $list = $ms.LprMatchListFolder.LprMatchLists | Where-Object Name -eq $Name
        if ($null -eq $list) {
            Write-Error "No LPR Match List found matching name '$Name'" -Category ObjectNotFound
            return
        }
        $allEntries = ($list | Get-LprMatchListEntry).RegistrationNumber
        $entriesToRemove = New-Object System.Collections.Generic.List[string]
        foreach ($number in $RegistrationNumber) {
            foreach ($entry in $allEntries) {
                if ($entry -like $number) {
                    $entriesToRemove.Add(($entry -replace '(?<!\\);','\;'))
                }
            }
        }

        if ($entriesToRemove.Count -gt 0 -and $PSCmdlet.ShouldProcess("LPR Match List '$Name'", "Delete rows")) {
            $data = [string]::Join(';', $entriesToRemove)
            $invokeResult = $list.MethodIdDeleteRegistrationNumbers($data)
            if ($invokeResult.State -ne 'Success') {
                Write-Error -Message $invokeResult.ErrorText -ErrorId $invokeResult.ErrorCode -Category ProtocolError
            }
        }
    }
}