Public/Import-LprMatchList.ps1

function Import-LprMatchList {
    <#
    .SYNOPSIS
        Imports the LPR Match List(s) found in the provided JSON file.
    .DESCRIPTION
        Imports the LPR Match List(s) found in the provided JSON file. Any LPR Match Lists defined in the file will be
        created if they don't already exist, and all properties of the match lists will be synchronized with the
        content of the file.
    .EXAMPLE
        PS C:\> Import-LprMatchList -Path .\lprlists.json
        Imports all LPR Match Lists in the file lprlists.json in the current directory
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        # Specifies the full path to the json file where the LPR Match List(s) should be exported
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]
        $Path,

        # Specifies that the LPR Match Lists should be passed back to the pipeline as they are created or updated
        [Parameter()]
        [switch]
        $PassThru
    )

    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 {
        $ms.LprMatchListFolder.ClearChildrenCache()
        $existingLists = $ms.LprMatchListFolder.LprMatchLists.Name
        $lists = Get-Content -Path $Path -Encoding UTF8 | ConvertFrom-Json
        foreach ($source in $lists) {
            if ($source.Name -eq 'Unlisted license plate') {
                Write-Verbose "Skipping $($source.Name) as it is the default list"
                continue
            }
            if ($source.Name -notin $existingLists) {
                if ($PSCmdlet.ShouldProcess("LPR Match List '$($destination.Name)", "Add")) {
                    Add-LprMatchList -Name $source.Name
                }
                else {
                    continue
                }
            }
            $destination = Get-LprMatchList -Name $source.Name
            if ($PSCmdlet.ShouldProcess("LPR Match List '$($destination.Name)", "Set properties")) {
                $destination.Description = $source.Description
                if ($null -eq $destination.Description) {
                    $destination.Description = [string]::Empty
                }
                $destination.CustomFields = $source.CustomFields
                if ($null -eq $destination.CustomFields) {
                    $destination.CustomFields = @()
                }
                $destination.Save()
            }

            if ($source.List.Count -gt 0 -and $PSCmdlet.ShouldProcess("LPR Match List '$($destination.Name)'", "Add LPR Match List Entries")) {
                $entries = $source.List | ConvertTo-LprMatchListEntryList
                $destination | Add-LprMatchListEntry -LprMatchListEntry $entries -Force
            }

            if ($PassThru) {
                Write-Output $destination
            }
        }
    }
}