Scripts/Get-BPAInstance.ps1
function Get-BPAInstance { <# .SYNOPSIS Gets AutoMate BPA workflow and task instances. .DESCRIPTION Get-BPAInstance gets instance objects from AutoMate BPA. Get-BPAInstance can receive items on the pipeline and return related objects. .PARAMETER InputObject The object(s) to use in search for instances. .PARAMETER ID The ID of the instance. .PARAMETER TransactionID The transaction ID of the instance. .PARAMETER StartDate The first date of events to retrieve (Default: 1 day ago). .PARAMETER EndDate The last date of events to retrieve (Default: now). .PARAMETER Status The status of the instance: All Completed Running Success Failed Stopped Paused Aborted Queued ResumedFromFailure .PARAMETER FilterSet The parameters to filter the search on. Supply hashtable(s) with the following properties: Property, Comparator, Value. Valid values for the Comparator are: =, !=, <, >, contains (default - no need to supply Comparator when using 'contains') .PARAMETER FilterSetMode If multiple filter sets are provided, FilterSetMode determines if the filter sets should be evaluated with an AND or an OR .PARAMETER SortProperty The object property to sort results on. Do not use BPAServer or TypeName, since those are custom properties added by this module, and not exposed in the API. .PARAMETER SortDescending If specified, this will sort the output on the specified SortProperty in descending order. Otherwise, ascending order is assumed. .PARAMETER IncludeRelative If instance is searched for using the -ID parameter, or when a workflow, task or process is piped in, related instances are also returned. .PARAMETER BPAServer The AutoMate BPA management server. .INPUTS Instances of the following objects can be retrieved by this function: Workflow Task Folder TaskAgent ProcessAgent .EXAMPLE # Get currently running instances Get-BPAInstance -Status Running .EXAMPLE # Get failed instances of workflow "My Workflow" Get-BPAWorkflow "My Workflow" | Get-BPAInstance -Status Failed .EXAMPLE # Get instances using filter sets Get-BPAInstance -FilterSet @{ Property = "ResultText"; Comparator = "contains"; Value = "FTP Workflow"} .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 08/24/2016 Date Modified : 05/01/2018 .LINK https://github.com/davidseibel/PoshBPA #> [CmdletBinding(DefaultParameterSetName = "All")] [OutputType([System.Object[]])] param( [Parameter(ValueFromPipeline = $true, ParameterSetName = "ByPipeline")] [ValidateNotNullOrEmpty()] $InputObject, [Parameter(Position = 0, ParameterSetName = "ByID")] [ValidateNotNullOrEmpty()] [string]$ID, [Parameter(ParameterSetName = "ByTransactionID")] [ValidateNotNullOrEmpty()] [string]$TransactionID, [ValidateNotNullOrEmpty()] [DateTime]$StartDate = (Get-Date).AddDays(-1), [ValidateNotNullOrEmpty()] [DateTime]$EndDate = (Get-Date), [ValidateNotNullOrEmpty()] [BPAInstanceStatus]$Status = [BPAInstanceStatus]::All, [Hashtable[]]$FilterSet, [ValidateSet("And","Or")] [string]$FilterSetMode = "And", [ValidateNotNullOrEmpty()] [switch]$IncludeRelative = $false, [ValidateNotNullOrEmpty()] [string[]]$SortProperty = "StartDateTime", [switch]$SortDescending = $false, [ValidateNotNullOrEmpty()] [string]$BPAServer ) BEGIN { if ($StartDate -gt $EndDate) { throw "StartDate must be before EndDate!" } $splat = @{ RestMethod = "Get" BPAServer = $BPAServer } # ID queries are case-sensitive if ($PSBoundParameters.ContainsKey("ID")) { $ID = $ID.ToLower() } if ($PSBoundParameters.ContainsKey("TransactionID")) { $TransactionID = $TransactionID.ToLower() } $result = @() } PROCESS { switch($PSCmdlet.ParameterSetName) { "All" { $restFunction = "list" $tempFilterSet = $FilterSet switch($Status) { "Running" { $restFunction = "running/list" } "Completed" { $restFunction = "completed/list" } default { if ($Status -ne [BPAInstanceStatus]::All) { $tempFilterSet += @{Property = "Status"; Comparator = "="; Value = $Status.value__} } } } $splat += @{ Resource = Format-BPAUri -Path "instances/$restFunction" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } $result = Invoke-BPARestMethod @splat } "ByID" { $tempFilterSet = $FilterSet + @{Property = "ID"; Comparator = "="; Value = $ID} $splat += @{ Resource = Format-BPAUri -Path "instances/list" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } $result = Invoke-BPARestMethod @splat } "ByTransactionID" { $tempFilterSet = $FilterSet + @{Property = "TransactionID"; Comparator = "="; Value = $TransactionID} $splat += @{ Resource = Format-BPAUri -Path "instances/list" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } $result = Invoke-BPARestMethod @splat } "ByPipeline" { foreach ($obj in $InputObject) { $thisStatus = $Status # Store the status temporarily, if multiple object types are on the pipeline (for example: workflows and instances) # this will make sure instances on the pipeline are always returned, regardless of status $tempSplat = $splat if (-not $tempSplat.ContainsKey("BPAServer")) { $tempSplat += @{ BPAServer = $obj.BPAServer } } else { $tempSplat["BPAServer"] = $obj.BPAServer } $tempResult = @() switch ($obj.TypeName) { {($_ -in @("Workflow","Task","Process"))} { $tempFilterSet = $FilterSet + @{Property = "ConstructID"; Comparator = "="; Value = $obj.ID} $tempSplat += @{ Resource = Format-BPAUri -Path "instances/list" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } $tempResult += Invoke-BPARestMethod @tempSplat } "Agent" { switch($Status) { "Running" { $tempSplat += @{ Resource = Format-BPAUri -Path "agents/$($obj.ID)/running_instances/list" -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } } default { $tempFilterSet = $FilterSet + @{Property = "AgentID"; Comparator = "="; Value = $obj.ID} $tempSplat += @{ Resource = Format-BPAUri -Path "instances/list" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $tempFilterSet -FilterSetMode $FilterSetMode -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool()} } } $tempResult += Invoke-BPARestMethod @tempSplat } "Folder" { $tempSplat += @{ Resource = Format-BPAUri -Path "instances/list" -RangeStart $StartDate -RangeEnd $EndDate -FilterSet $FilterSet -FilterSetMode $FilterSetMode -FolderID $obj.ID -IncludeRelativeInstances:$IncludeRelative.ToBool() -SortProperty $SortProperty -SortDescending:$SortDescending.ToBool() } $tempResult += Invoke-BPARestMethod @tempSplat } "Instance" { $thisStatus = [BPAInstanceStatus]::All $tempResult += Get-BPAInstance -ID $obj.ID -Status All -StartDate $StartDate -EndDate $EndDate -BPAServer $obj.BPAServer -IncludeRelative:$IncludeRelative.ToBool() } default { $unsupportedType = $obj.GetType().FullName if ($_) { $unsupportedType = $_ } elseif (($null -ne $obj.Type) -and ($obj.Type -ne "")) { $unsupportedType = $obj.Type } Write-Error -Message "Unsupported input type '$unsupportedType' encountered!" -TargetObject $obj } } switch($thisStatus) { "All" { $result += $tempResult } "Completed" { $result += $tempResult | Where-Object {$_.Status -ne [BPAInstanceStatus]::Running.value__} } default { $result += $tempResult | Where-Object {$_.Status -eq $thisStatus.value__} } } } } } } END { $SortProperty += "BPAServer", "ID" return $result | Sort-Object $SortProperty -Unique -Descending:$SortDescending.ToBool() } } |