Scripts/Copy-BPAWorkflow.ps1
function Copy-BPAWorkflow { <# .SYNOPSIS Copies an AutoMate BPA workflow. .DESCRIPTION Copy-BPAWorkflow can copy a workflow within a server. .PARAMETER InputObject The object to copy. .PARAMETER Name The new name to set on the object. .PARAMETER Folder The folder to place the object in. .INPUTS The following BPA object types can be modified by this function: Workflow .EXAMPLE # Copy workflow "FTP Files to Company A" to "FTP Files to Company B" Get-BPAWorkflow "FTP Files to Company A" | Copy-BPAWorkflow -Name "FTP Files to Company B" -Folder (Get-BPAFolder WORKFLOWS) .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 11/09/2016 Date Modified : 02/08/2018 .LINK https://github.com/davidseibel/PoshBPA #> [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] $InputObject, [ValidateNotNullOrEmpty()] [string]$Name, [ValidateScript({$_.TypeName -eq "Folder"})] $Folder ) BEGIN { $currentDate = Get-Date $nullDate = Get-Date "12/31/1899 7:00:00 PM" } PROCESS { foreach ($obj in $InputObject) { if ($obj.TypeName -eq "Workflow") { $guid = "{$((New-Guid).Guid)}" $user = Get-BPAUser -BPAServer $obj.BPAServer | Where-Object {$_.Name -ieq ($BPAConnectionInfo | Where-Object {$_.Server -eq $BPAServer}).Credential.UserName} $copy = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer $copy.ID = $guid $copy.CompletionState = 0 $copy.CreatedBy = $user.ID $copy.CreatedOn = $currentDate $copy.EndedOn = $nullDate $copy.ResultCode = 0 $copy.ResultText = "" $copy.ModifiedOn = $currentDate $copy.StartedOn = $nullDate $copy.Version = 1 $copy.VersionDate = $currentDate # Update GUIDs foreach ($link in $copy.Links) { $link.ID = "{$((New-Guid).Guid)}" $link.WorkflowID = $guid } foreach ($item in $copy.Items) { # Update item ID and any link references $newGuid = "{$((New-Guid).Guid)}" $copy.Links | Where-Object {$_.SourceID -eq $item.ID} | ForEach-Object { $_.SourceID = $newGuid } $copy.Links | Where-Object {$_.DestinationID -eq $item.ID} | ForEach-Object { $_.DestinationID = $newGuid } $item.ID = $newGuid $item.WorkflowID = $guid } foreach ($trigger in $copy.Triggers) { # Update trigger ID and any link references $newGuid = "{$((New-Guid).Guid)}" $copy.Links | Where-Object {$_.SourceID -eq $trigger.ID} | ForEach-Object { $_.SourceID = $newGuid } $copy.Links | Where-Object {$_.DestinationID -eq $trigger.ID} | ForEach-Object { $_.DestinationID = $newGuid } $trigger.ID = $newGuid $trigger.WorkflowID = $guid } foreach ($variable in $copy.Variables) { $variable.ID = "{$((New-Guid).Guid)}" $variable.ParentID = $guid } if ($Name) { $copy.Name = $Name } if ($Folder) { $copy.ParentID = $Folder.ID } $copy | New-BPAObject -BPAServer $obj.BPAServer Get-BPAWorkflow -ID $guid -BPAServer $obj.BPAServer } else { Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj } } } } |