Scripts/Stop-BPAInstance.ps1
function Stop-BPAInstance { <# .SYNOPSIS Stops AutoMate BPA workflow and task instances. .DESCRIPTION Stop-BPAInstance stops running workflow and task instances. .PARAMETER InputObject The instances to stop. .INPUTS Instances can be supplied on the pipeline to this function. .EXAMPLE # Stops all currently running instances Get-BPAInstance -Status Running | Stop-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(SupportsShouldProcess=$true,DefaultParameterSetName = "All")] [OutputType([System.Object[]])] param( [Parameter(ValueFromPipeline = $true, ParameterSetName = "ByPipeline")] [ValidateNotNullOrEmpty()] $InputObject ) PROCESS { foreach ($obj in $InputObject) { switch ($obj.TypeName) { "Instance" { switch ($obj.Status -as [BPAInstanceStatus]) { {($_ -in @("Running","Paused","Queued"))} { switch ($obj.ConstructType -as [BPAConstructType]) { "Workflow" { if ($PSCmdlet.ShouldProcess($obj.BPAServer, "Stopping Workflow instance: '$($obj.ConstructName)'")) { Invoke-BPARestMethod -Resource "workflows/$($obj.ConstructID)/running_instances/$($obj.ID)/stop" -RestMethod Post -BPAServer $obj.BPAServer | Out-Null Start-Sleep 1 # The instance can't be retrieved right away, have to pause briefly try { Invoke-BPARestMethod -Resource ('instances/list?filter_sets="ID","=","\"' + $obj.ID + '\""') -RestMethod Get -BPAServer $obj.BPAServer } catch { Write-Error -Message "Exception occurred while stopping workflow instance $($obj.ID): $_" -TargetObject $obj } } } "Task" { if ($PSCmdlet.ShouldProcess($obj.BPAServer, "Stopping Task instance: '$($obj.ConstructName)'")) { Invoke-BPARestMethod -Resource "tasks/$($obj.ConstructID)/running_instances/$($obj.ID)/stop" -RestMethod Post -BPAServer $obj.BPAServer | Out-Null Start-Sleep 1 # The instance can't be retrieved right away, have to pause briefly try { Invoke-BPARestMethod -Resource ('instances/list?filter_sets="ID","=","\"' + $obj.ID + '\""') -RestMethod Get -BPAServer $obj.BPAServer } catch { Write-Error -Message "Exception occurred while stopping task instance $($obj.ID): $_" -TargetObject $obj } } } 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 } } } } } |