Public/Wait-PdqClipboardData.ps1

<#
.SYNOPSIS
Waits for a specific type of data to appear on the clipboard.
 
.DESCRIPTION
Continuously queries the clipboard to see if it contains the specified format.
If the timeout is exceeded, an error will be returned.
 
.INPUTS
None.
 
.OUTPUTS
None.
 
.EXAMPLE
Wait-PdqClipboardData -Format 'Collection'
Waits for a PDQ Inventory Collection to appear on the clipboard.
#>

function Wait-PdqClipboardData {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        # Run Get-PdqClipboardFormat to get a list of formats.
        [String]$Format,

        # The maximum number of seconds you are willing to wait.
        [UInt32]$Timeout = 300
    )

    $FormatData = Get-PdqClipboardFormat -Format $Format
    
    # Clear the clipboard in case there is already PDQ data on it.
    [System.Windows.Forms.Clipboard]::Clear()
    
    # Wait for the clipboard to contain data in the desired format.
    $StopWatch = [System.Diagnostics.Stopwatch]::StartNew()
    While (-not [System.Windows.Forms.Clipboard]::ContainsData($FormatData.Name)) {

        Start-Sleep -Milliseconds 250

        if ( $StopWatch.Elapsed.TotalSeconds -ge $Timeout ) {

            $StopWatch.Stop()
            throw 'Exceeded timeout for Wait-PdqClipboard.'

        }

    }
    $StopWatch.Stop()

}