Scripts/Suspend-BPAInstance.ps1

function Suspend-BPAInstance {
    <#
        .SYNOPSIS
            Pauses AutoMate BPA workflow and task instances.
 
        .DESCRIPTION
            Suspend-BPAInstance suspends running workflow and task instances.
 
        .PARAMETER InputObject
            The instances to suspend.
 
        .INPUTS
            Instances can be supplied on the pipeline to this function.
 
        .EXAMPLE
            # Suspend all currently running instances
            Get-BPAInstance -Status Running | Suspend-BPAInstance
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 09/14/2016
            Date Modified : 02/27/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

    PROCESS {
        foreach ($obj in $InputObject) {
            switch ($obj.TypeName) {
                "Instance" {
                    switch ($obj.Status -as [BPAInstanceStatus]) {
                        "Running" {
                            switch ($obj.ConstructType -as [BPAConstructType]) {
                                "Workflow" {
                                    Invoke-BPARestMethod -Resource "workflows/$($obj.ConstructID)/running_instances/$($obj.ID)/pause" -RestMethod Post -BPAServer $obj.BPAServer | Out-Null
                                    Start-Sleep 1   # The instance can't be retrieved right away, have to pause briefly
                                    Invoke-BPARestMethod -Resource ('instances/list?filter_sets="ID","=","\"' + $obj.ID + '\""') -RestMethod Get -BPAServer $obj.BPAServer
                                }
                                "Task" {
                                    Invoke-BPARestMethod -Resource "tasks/$($obj.ConstructID)/running_instances/$($obj.ID)/pause" -RestMethod Post -BPAServer $obj.BPAServer | Out-Null
                                    Start-Sleep 1   # The instance can't be retrieved right away, have to pause briefly
                                    Invoke-BPARestMethod -Resource ('instances/list?filter_sets="ID","=","\"' + $obj.ID + '\""') -RestMethod Get -BPAServer $obj.BPAServer
                                }
                                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
                }
            }
        }
    }
}