Public/Connect-PdqInventoryScanner.ps1

<#
.SYNOPSIS
Ties a Scanner to a Scan Profile by reading the contents of the clipboard.
 
.DESCRIPTION
When this function executes, it will clear your clipboard and give you a couple of prompts.
It retrieves IDs from the data you copied to your clipboard, then accesses your database.
The Scanner ID and Scan Profile ID will be inserted into the ScanProfileScanner table.
 
.INPUTS
None.
 
.OUTPUTS
None.
 
.EXAMPLE
Connect-PdqInventoryScanner
Displays a couple of prompts, then inserts a row into your database.
#>

function Connect-PdqInventoryScanner {

    [CmdletBinding()]
    param (
    )

    # https://www.reddit.com/r/pdq/comments/iyfsku/experimental_scanners_can_be_tied_to_multiple/
    Assert-PdqMinimumVersion -Product 'Inventory' -MinimumVersion '19.2.68.0'

    Write-Host ''
    Write-Host 'Copy the Scanner you would like to tie to a different Scan Profile.'
    Wait-PdqClipboardData -Format 'Scanner'

    $Scanner = Get-PdqClipboardData -Format 'Scanner'
    $ScannerId = $Scanner.SourceScannerId
    if ( -not $ScannerId ) {

        throw 'Unable to retrieve the Scanner ID from the clipboard.'

    }

    Write-Host "Copy the Scan Profile you would like to tie ScannerId $ScannerId to."
    Wait-PdqClipboardData -Format 'ScanProfile'

    $ScanProfile = Get-PdqClipboardData -Format 'ScanProfile'
    $ScanProfileId = $ScanProfile.'AdminArsenal.Export'.ScanProfile.ScanProfileId.value
    $ScanProfileName = $ScanProfile.'AdminArsenal.Export'.ScanProfile.Name
    if ( -not $ScanProfileId ) {

        throw 'Unable to retrieve the Scan Profile ID from the clipboard.'

    }
    Write-Verbose "Scan Profile ID: $ScanProfileId"
    Write-Verbose "Scan Profile Name: $ScanProfileName"

    Try {

        $CloseConnection = Open-PdqSqlConnection -Product 'Inventory'

        $CurrentScanProfilesQuery = "SELECT ScanProfileId FROM ScanProfileScanner WHERE ScannerId = $ScannerId;"
        $CurrentScanProfiles = (Invoke-SqlQuery -Query $CurrentScanProfilesQuery -ConnectionName 'Inventory').ScanProfileId
        Write-Verbose "Scanner ID $ScannerId is currently in these Scan Profiles: $($CurrentScanProfiles -join ', ')"
        if ( $ScanProfileId -in $CurrentScanProfiles ) {

            throw "Scan Profile '$ScanProfileName' already contains ScannerId $ScannerId."

        }

        $ScanProfileScannerQuery = "INSERT INTO ScanProfileScanner (ScanProfileId, ScannerId) VALUES ($ScanProfileId, $ScannerId)"
        $null = Invoke-SqlUpdate -Query $ScanProfileScannerQuery -ConnectionName 'Inventory'

        Write-Host ''
        Write-Host 'Success! Please refresh the PDQ Inventory console.'

    } Finally {

        Close-PdqSqlConnection -Product 'Inventory' -CloseConnection $CloseConnection

    }

}