Public/Wait-PdqDeployment.ps1

<#
.SYNOPSIS
Waits for the specified deployments to finish.
 
.INPUTS
None.
 
.OUTPUTS
None.
 
.EXAMPLE
Wait-PdqDeployment -Id 42, 43
Waits until both deployments have finished.
#>


function Wait-PdqDeployment {

    [CmdletBinding()]
    param (
        # The IDs of the deployments you would like to wait for.
        [UInt32[]]$Id,

        # The number of milliseconds you would like to wait for between database queries.
        [Int32]$SleepMilliseconds = 1000,

        # The path to the currently active database will be retrieved by default.
        # You can use this parameter if you wish to run this function against a different database.
        [String]$DatabasePath
    )

    try {

        $CloseConnection = Open-PdqSqlConnection -Product 'Deploy' -DatabasePath $DatabasePath

        do {

            $Deployments = Get-PdqDeployment -Id $Id -DatabasePath $DatabasePath | Where-Object 'Status' -eq 'Running'
            'Waiting for {0} deployment(s) to finish' -f $Deployments.Count | Write-Verbose
            $Id = $Deployments.DeploymentId

            if ( $Deployments ) {

                Start-Sleep -Milliseconds $SleepMilliseconds

            }


        } until ( -not $Deployments )

    } finally {

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

    }

}