Scripts/Set-BPAWorkflow.ps1

function Set-BPAWorkflow {
    <#
        .SYNOPSIS
            Sets properties of an AutoMate BPA workflow.
 
        .DESCRIPTION
            Set-BPAWorkflow can change properties of a workflow object.
 
        .PARAMETER InputObject
            The object to modify.
 
        .PARAMETER Instance
            A modified workflow to save to BPA.
 
        .PARAMETER Notes
            The new notes to set on the object.
 
        .PARAMETER CompletionState
            The completion state (staging level) to set on the object.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            Workflow
 
        .EXAMPLE
            # Change notes for a workflow
            Get-BPAWorkflow "Some Workflow" | Set-BPAWorkflow -Notes "Does something important"
 
        .EXAMPLE
            # Modify a workflow
            Set-BPAWorkflow -Instance $modifiedWorkflow
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 11/07/2016
            Date Modified : 07/02/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [Parameter(Mandatory = $true, ParameterSetName = "ByInstance")]
        [ValidateScript({
            if ($_.TypeName -eq "Workflow") {
                $true
            } else {
                throw [System.Management.Automation.PSArgumentException]"Instance is not a Workflow!"
            }
        })]
        $Instance,

        [Parameter(ParameterSetName = "ByInputObject")]
        [AllowEmptyString()]
        [string]$Notes,

        [Parameter(ParameterSetName = "ByInputObject")]
        [BPACompletionState]$CompletionState
    )
    PROCESS {
        switch ($PSCmdlet.ParameterSetName) {
            "ByInputObject" {
                foreach ($obj in $InputObject) {
                    if ($obj.TypeName -eq "Workflow") {
                        $updateObject = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer
                        $shouldUpdate = $false
        
                        if ($PSBoundParameters.ContainsKey("Notes")) {
                            if ($updateObject.Notes -ne $Notes) {
                                $updateObject.Notes = $Notes
                                $shouldUpdate = $true
                            }
                        }
                        if ($PSBoundParameters.ContainsKey("CompletionState")) {
                            if ($updateObject.CompletionState -ne $CompletionState.value__) {
                                $updateObject.CompletionState = $CompletionState.value__
                                $shouldUpdate = $true
                            }
                        }
                        if ($shouldUpdate) {
                            $updateObject | Set-BPAObject
                        } else {
                            Write-Verbose "$($obj.TypeName) '$($obj.Name)' already contains the specified values."
                        }
                    } else {
                        Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
                    }
                }
            }
            "ByInstance" {
                $updateObject = $Instance.PSObject.Copy()
                $validated = $true
                if ($updateObject.ID -notmatch $BPAGuidRegex) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has an ID that is not a valid GUID!"
                }
                if ($updateObject.Items | Group-Object ID | Where-Object {$_.Count -gt 1}) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has multiple items with the same ID!"
                }
                if ($updateObject.Triggers | Group-Object ID | Where-Object {$_.Count -gt 1}) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has multiple triggers with the same ID!"
                }
                if ($updateObject.Links | Group-Object ID | Where-Object {$_.Count -gt 1}) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has multiple links with the same ID!"
                }
                if ($updateObject.Variables | Group-Object ID | Where-Object {$_.Count -gt 1}) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has multiple variables with the same ID!"
                }
                foreach ($item in ($updateObject.Items + $updateObject.Triggers + $updateObject.Links)) {
                    if ($item.ID -notmatch $BPAGuidRegex) {
                        $validated = $false
                        throw "$($Instance.TypeName) '$($Instance.Name)' contains an item with an ID that is not a valid GUID!"
                    }
                    if ($item.WorkflowID -ne $updateObject.ID) {
                        $validated = $false
                        throw "$($Instance.TypeName) '$($Instance.Name)' contains an item with an invalid workflow ID!"
                    }
                }
                foreach ($variable in $updateObject.Variables) {                    
                    if ($variable.ID -notmatch $BPAGuidRegex) {
                        $validated = $false
                        throw "$($Instance.TypeName) '$($Instance.Name)' contains a variable with an ID that is not a valid GUID!"
                    }
                    if ($variable.ParentID -ne $updateObject.ID) {
                        $validated = $false
                        throw "$($Instance.TypeName) '$($Instance.Name)' contains a variable with an invalid parent ID!"
                    }
                    if (($variable.Name -eq '') -or ($variable.Name -eq $null)) {
                        $validated = $false
                        throw "$($Instance.TypeName) '$($Instance.Name)' contains a variable with an invalid name!"                        
                    }
                }
                if ($validated) {
                    $updateObject | Set-BPAObject
                }
            }
        }        
    }
}