Scripts/Find-BPAObject.ps1

function Find-BPAObject {
    <#
        .SYNOPSIS
            Finds objects in AutoMate BPA.
 
        .DESCRIPTION
            Find-BPAObject searches AutoMate BPA for objects based on a search query.
 
        .PARAMETER Pattern
            The pattern to use when searching. Regular expressions are supported.
 
        .PARAMETER Type
            The object type(s) to perform search against.
 
        .PARAMETER SearchDepth
            The number of layers (sub-objects/properties) in every object to search. Using a search depth that is too deep can significantly increase search time.
 
        .PARAMETER BPAServer
            The server to perform search against.
 
        .EXAMPLE
            # Find all tasks that have FTP activities
            Find-BPAObject -Pattern "AMFTP" -Type Task
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 06/28/2017
            Date Modified : 03/01/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $true)]
        [string]$Pattern,

        [ValidateSet("Workflow","Task","Condition","Process","TaskAgent","ProcessAgent","AgentGroup","User","UserGroup")]
        [string[]]$Type = @("Workflow","Task","Condition","Process","TaskAgent","ProcessAgent","AgentGroup","User","UserGroup"),

        [ValidateScript({$_ -gt 0})]
        [int]$SearchDepth = 3,

        [ValidateNotNullOrEmpty()]
        [string]$BPAServer
    )
    
    $splat = @{
        RestMethod = "Get"
        BPAServer  = $BPAServer
    }

    # Retrieve all objects of the specified types
    $toSearch = @()
    foreach ($t in $Type) {
        $tempSplat = $splat
        $tempSplat += @{ Resource = "$(([BPATypeDictionary]::($t)).RestResource)/list?sort_field=Name" }
        $toSearch += Invoke-BPARestMethod @tempSplat
    }

    # Recursively search properties of objects
    $searchResult = $toSearch | Search-ObjectProperty -Pattern $Pattern
    $searchResult | Foreach-Object {$_.PSObject.TypeNames.Insert(0,"BPAConstructType.FoundObject")}

    return $searchResult
}