Public/Add-LprMatchListEntry.ps1

function Add-LprMatchListEntry {
    <#
    .SYNOPSIS
        Adds or updates data from one or more [LprMatchListEntry] objects to a given LPR Match List
    .DESCRIPTION
        Adds the data from one or more [LprMatchListEntry] objects to a given LPR Match List. If a registration number
        is already added with the same value, the custom fields will be updated if the values are different.
 
        The MIP SDK does not differentiate between adding new entries to a list or updating existing entries, so this
        function will do both.
    .EXAMPLE
        PS C:\> $entry = New-LprMatchListEntry -RegistrationNumber ABC123 -CustomFields @{ EmployeeId = '456' }
        PS C:\> Add-LprMatchListEntry -Name Staff -LprMatchListEntry $entry
        Adds a new LPR Match List entry to the match list named 'Staff'
    #>

    [CmdletBinding()]
    param(
        # Specifies the name of the LPR Match List
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]
        $Name,

        # Specifies one or more Match List Entries.
        # Note: Use New-LprMatchListEntry to create a new entry and use Add-LprMatchListEntry to add it to a given match list
        [Parameter(Mandatory)]
        [LprMatchListEntry[]]
        $LprMatchListEntry,

        # Specifies that the entry should be added even if the LPR Match List does not include one or more custom
        # fields defined in the LprMatchListEntry property. Custom fields that do not exist in the VMS already will be
        # ignored.
        [Parameter()]
        [switch]
        $Force
    )

    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
        }

        $customFields = New-Object System.Collections.Generic.List[string]
        $customFields.AddRange($list.CustomFields)
        $rows = New-Object System.Collections.Generic.List[string]
        $row = New-Object System.Collections.Generic.List[string]
        foreach ($entry in $LprMatchListEntry) {
            $row.Clear()
            $escapedValue = $entry.RegistrationNumber -replace '(?<!\\),','\,'
            $escapedValue = $escapedValue -replace '(?<!\\);','\;'
            $row.Add($escapedValue)

            foreach ($key in $entry.CustomFields.Keys | Where-Object { $_ -notin $list.CustomFields }) {
                if (-not $Force) {
                    Write-Error "A Custom Field with the name of '$key' was found on registration number '$($entry.RegistrationNumber)' but it does not exist in the specified LPR Match List. Run this command with -Force to add/update the entry and add the field to the list of custom fields."
                    return
                }
                $customFields.Add($key)
                $list.CustomFields = $customFields
                $list.Save()
            }

            foreach ($field in $list.CustomFields) {
                $customFieldValue = $entry.CustomFields.$field
                if ($null -eq $customFieldValue) {
                    $customFieldValue = [string]::Empty
                }
                $escapedValue = $customFieldValue -replace '(?<!\\),','\,'
                $escapedValue = $escapedValue -replace '(?<!\\);','\;'
                $row.Add($escapedValue)
            }
            $rows.Add([string]::Join(',', $row))
        }
        $data = [string]::Join(';', $rows)
        if ([string]::IsNullOrWhiteSpace($data)) {
            return
        }

        $invokeResult = $list.MethodIdAddOrEditRegistrationNumbersInfo($data)
        if ($invokeResult.State -ne 'Success') {
            Write-Error -Message $invokeResult.ErrorText -ErrorId $invokeResult.ErrorCode -Category ProtocolError
        }
    }
}