Public/Move-PdqDeployPackage.ps1

<#
.SYNOPSIS
Moves a package into a folder by reading the contents of the clipboard.
 
.DESCRIPTION
Created for https://help.pdq.com/hc/en-us/community/posts/360074687911
Reads the contents of your clipboard to get data about the package and the folder you want to move it into,
then updates the Path and FolderId of the package in your database.
 
.INPUTS
None.
 
.OUTPUTS
None.
 
.EXAMPLE
Move-PdqDeployPackage
Displays a couple of prompts, then updates your database.
#>

function Move-PdqDeployPackage {

    [CmdletBinding()]
    param (
    )

    # This is just a guess. I have no idea what the oldest compatible version is.
    Assert-PdqMinimumVersion -Product 'Deploy' -MinimumVersion '1.0.0.0'

    Write-Host ''
    Write-Host 'Copy the package you would like to move.'
    Wait-PdqClipboard -Format 'Package'

    $Package = Get-PdqClipboardData -Format 'Package'
    $PackagePath = $Package.'AdminArsenal.Export'.Package.Path
    $PackageName = $Package.'AdminArsenal.Export'.Package.Name
    if ( -not $PackagePath ) {

        throw 'Unable to retrieve the package path from the clipboard.'

    }
    if ( -not $PackageName ) {

        throw 'Unable to retrieve the package name from the clipboard.'

    }

    Write-Host "Copy the folder you would like to move '$PackagePath' into."
    Wait-PdqClipboard -Format 'DeployFolder'

    $Folder = Get-PdqClipboardData -Format 'DeployFolder'
    $FolderId = $Folder.'AdminArsenal.Export'.Folder.Id.value
    $FolderPath = $Folder.'AdminArsenal.Export'.Folder.Path
    if ( -not $FolderId ) {

        throw 'Unable to retrieve the folder id from the clipboard.'

    }
    if ( -not $FolderPath ) {

        throw 'Unable to retrieve the folder path from the clipboard.'

    }

    Try {

        $CloseConnection = Open-PdqSqlConnection -Product 'Deploy'

        $PackageIdQuery = "SELECT PackageId FROM Packages WHERE Path = '$PackagePath';"
        $PackageId = (Invoke-SqlQuery -Query $PackageIdQuery -ConnectionName 'Deploy').PackageId
        Switch ( $PackageId.Count ) {

            0 {

                throw "Unable to find a PackageId for '$PackagePath'."

            }

            {$_ -gt 1} {

                throw "Multiple IDs were returned for '$PackagePath'."

            }

        }

        $PackageNewPath = "$FolderPath\$PackageName"
        $Query = "UPDATE Packages SET (FolderId, Path) = ($FolderId, '$PackageNewPath') WHERE PackageId = $PackageId;"
        Write-Verbose $Query
        $RowsAffected = Invoke-SqlUpdate -Query $Query -ConnectionName 'Deploy'
        if ( $RowsAffected -ne 1 ) {

            throw "$RowsAffected rows affected."

        }

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

    } Finally {

        Close-PdqSqlConnection -Product 'Deploy' -CloseConnection $CloseConnection

    }

}