Scripts/Switch-BPAWorkflowItem.ps1

function Switch-BPAWorkflowItem {
    <#
        .SYNOPSIS
            Replaces items in a BPA workflow
 
        .DESCRIPTION
            Switch-BPAWorkflowItem can replace items in a workflow object.
 
        .PARAMETER InputObject
            The object to replace items in.
 
        .PARAMETER CurrentItem
            The current item to replace.
 
        .PARAMETER NewItem
            The new item to replace the current item with.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            Workflow
 
        .EXAMPLE
            # Replace instances of the "Copy Files" task with "Move Files" in workflow "FTP Files"
            Get-BPAWorkflow "FTP Files" | Switch-BPAWorkflowItem -CurrentItem (Get-BPATask "Copy Files") -NewItem (Get-BPATask "Move Files")
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 07/24/2017
            Date Modified : 02/08/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject,

        [Parameter(Mandatory = $true)]
        $CurrentItem,

        [Parameter(Mandatory = $true)]
        $NewItem
    )

    BEGIN {
        if ($CurrentItem.BPAServer -eq $NewItem.BPAServer) {
            if ($CurrentItem.ID -eq $NewItem.ID) {
                throw "CurrentItem and NewItem are the same!"
            }
            if ($CurrentItem.Type -ne $NewItem.Type) {
                throw "CurrentItem and NewItem are not the same type!"
            }
        } else {
            throw "CurrentItem and NewItem are not on the same BPA server!"
        }
    }

    PROCESS {
        foreach ($obj in $InputObject) {
            if ($obj.TypeName -eq "Workflow") {
                if ($obj.BPAServer -ne $CurrentItem.BPAServer) {
                    throw "CurrentItem '$($CurrentItem.Name)' ($($CurrentItem.BPAServer)) is not on the same server as '$($obj.Name)' ($($obj.BPAServer))!"
                    break
                }
                if ($obj.BPAServer -ne $NewItem.BPAServer) {
                    throw "NewItem '$($NewItem.Name)' ($($NewItem.BPAServer)) is not on the same server as '$($obj.Name)' ($($obj.BPAServer))!"
                    break
                }
                $update = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer
                $found = $false
                foreach ($item in $update.Items) {
                    if ($item.ConstructID -eq $CurrentItem.ID) {
                        $item.ConstructID = $NewItem.ID
                        $found = $true
                    }
                }
                foreach ($trigger in $update.Triggers) {
                    if ($trigger.ConstructID -eq $CurrentItem.ID) {
                        $trigger.ConstructID = $NewItem.ID
                        $found = $true
                    }
                }
                if ($found) {
                    $update | Set-BPAObject
                }
            } else {
                Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
            }
        }
    }
}