Scripts/Wait-BPAInstance.ps1

function Wait-BPAInstance {
    <#
        .SYNOPSIS
            Waits for AutoMate BPA workflow and task instances to complete.
 
        .DESCRIPTION
            Wait-BPAInstance waits for running workflow and task instances to complete.
 
        .PARAMETER InputObject
            The instances to wait for.
 
        .PARAMETER Timeout
            Seconds to wait for the instance to complete before timing out.
 
        .INPUTS
            Instances can be supplied on the pipeline to this function.
 
        .EXAMPLE
            # Suspend all currently running instances
            Get-BPAInstance -Status Running | Wait-BPAInstance
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 09/23/2016
            Date Modified : 02/27/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(DefaultParameterSetName = "All")]
    [OutputType([System.Object[]])]
    param(
        [Parameter(ValueFromPipeline = $true, ParameterSetName = "ByPipeline")]
        [ValidateNotNullOrEmpty()]
        $InputObject,

        [ValidateNotNullOrEmpty()]
        $Timeout
    )

    BEGIN {
        if ($Timeout) {
            $startTime = Get-Date
        }
    }

    PROCESS {
        foreach ($obj in $InputObject) {
            switch ($obj.TypeName) {
                "Instance" {
                    switch ($obj.Status -as [BPAInstanceStatus]) {
                        "Running" {
                            switch ($obj.ConstructType -as [BPAConstructType]) {
                                {($_ -in @("Workflow","Task"))} {
                                    $complete = $false
                                    while (-not $complete) {
                                        $temp = Invoke-BPARestMethod -Resource ('instances/list?filter_sets="ID","=","\"' + $obj.ID + '\""') -RestMethod Get -BPAServer $obj.BPAServer
                                        if (($temp.Status -as [BPAInstanceStatus]) -eq "Running") {
                                            Write-Verbose "Waiting for $($temp.ConstructType -as [BPAConstructType]) instance '$($temp.ConstructName)' to complete..."
                                            if ($Timeout) {
                                                $now = Get-Date
                                                if (($now - $startTime).Seconds -gt $Timeout) {
                                                    $complete = $true
                                                    Write-Error -Message "Timed out after $Timeout seconds waiting for $($temp.ConstructType -as [BPAConstructType]) instance '$($temp.ConstructName) to complete!" -TargetObject $temp
                                                }
                                            }
                                            Start-Sleep 1
                                        } else {
                                            $complete = $true
                                            Write-Verbose "Complete!"
                                        }
                                    }
                                    $temp
                                }
                                default {
                                    if ($_) { $message = "Unsupported construct type '$_' encountered! Workflow: $($obj.Name)"           }
                                    else    { $message = "Unsupported construct type '$($obj.Type)' encountered! Workflow: $($obj.Name)" }
                                    Write-Error -Message $message -TargetObject $obj
                                }
                            }
                        }
                        default {
                            Write-Error -Message "Instance $($obj.ID) is not running! Status: $_" -TargetObject $obj
                        }
                    }
                }
                default {
                    Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
                }
            }
        }
    }
}