Public/Tools/Select-Camera.ps1

function Select-Camera {
    <#
    .SYNOPSIS
        Offers a UI dialog for selecting items, similar to the item selection interface in Management Client.
 
    .DESCRIPTION
        This cmdlet implements the VideoOS.Platform.UI.ItemPickerUserControl in a custom form to allow the user to
        select one or more items of any kind using a friendly and customizable user interface.
 
    .Parameter Title
        Specifies the text in the title-bar of the Item Picker window. The default is "Select Camera(s)".
     
    .Parameter SingleSelect
        The ItemPicker allows for multiple items by default. Supply this parameter to force selection of a single item.
 
    .Parameter AllowFolders
        Device groups are considered folders and are not selectable by default. To allow for selecting many items with one click,
        include this parameter. Consider using this with the FlattenOutput switch unless you specifically need to select a folder
        item instead of it's child items.
 
    .Parameter AllowServers
        Supply this switch to enable selection of servers. You might choose to do this if you want to select Recording Servers,
        or you want to select all child items, such as cameras, from a server. Consider using this with the FlattenOutput switch
        unless you specifically need to select a server item instead of it's child items.
 
    .Parameter RemoveDuplicates
        Automatically remove duplicate cameras from the output before outputing them. Useful when you select a folder which may
        have the same cameras in more than one child folder.
     
    .Parameter OutputAsItem
        Output cameras as VideoOS.Platform.Item objects instead of converting them to Configuration API Camera objects. Depending
        on your needs, it may be more performant to use OutputAsItem. For example, if you are using a cmdlet like Get-Snapshot, you
        can extract the $item.FQID.ObjectId and provide that Guid in the CameraId parameter to avoid an unnecessary conversion
        between Item, ConfigurationItem, and back again.
 
    .EXAMPLE
        PS C:\> Select-Camera -AllowFolders -AllowServers -RemoveDuplicates
 
        Launch the Item Picker and allow the user to add servers or whole groups/folders. The output will be de-duplicated in the
        event the user-defined groups have the same camera(s) present in more than one child folder. The objects returned will be
        the same kind of object returned by the Get-Camera cmdlet.
 
    .EXAMPLE
        PS C:\> Select-Camera -OutputAsItem | % { Get-Snapshot -CameraId $_.FQID.ObjectId -Live }
 
        Launch the Item Picker and use the resulting Item.FQID.ObjectId properties of the camera(s) to get a live snapshot from
        the Get-Snapshot cmdlet.
    #>

    [CmdletBinding()]
    param(
        [Parameter()]
        [string]
        $Title = "Select Camera(s)",
        [Parameter()]
        [switch]
        $SingleSelect,
        [Parameter()]
        [switch]
        $AllowFolders,
        [Parameter()]
        [switch]
        $AllowServers,
        [Parameter()]
        [switch]
        $RemoveDuplicates,
        [Parameter()]
        [switch]
        $OutputAsItem
    )
    process {
        $items = Select-VideoOSItem -Title $Title -Kind ([VideoOS.Platform.Kind]::Camera) -AllowFolders:$AllowFolders -AllowServers:$AllowServers -SingleSelect:$SingleSelect -FlattenOutput
        $processed = @{}
        if ($RemoveDuplicates) {
            foreach ($item in $items) {
                if ($processed.ContainsKey($item.FQID.ObjectId)) {
                    continue
                }
                $processed.Add($item.FQID.ObjectId, $null)
                if ($OutputAsItem) {
                    Write-Output $item
                }
                else {
                    Get-Camera -Id $item.FQID.ObjectId
                }
            }
        }
        else {
            if ($OutputAsItem) {
                Write-Output $items
            }
            else {
                Write-Output ($items | ForEach-Object { Get-Camera -Id $_.FQID.ObjectId })
            }
        }
    }
}