Scripts/Set-BPAWorkflowItem.ps1

function Set-BPAWorkflowItem {
    <#
        .SYNOPSIS
            Sets an item in a BPA workflow
 
        .DESCRIPTION
            Set-BPAWorkflowItem can modify an item in a workflow object.
 
        .PARAMETER InputObject
            The object to modify - a workflow or a workflow variable.
 
        .PARAMETER ID
            The ID of the item to modify (if passing in a workflow).
 
        .PARAMETER Enabled
            If the item is enabled or not.
 
        .PARAMETER UseLabel
            If the item should use the configured label or not.
 
        .PARAMETER Label
            The label to place on the item (specify -UseLabel) to show the label in the workflow designer.
 
        .PARAMETER Height
            The height of the object in the workflow designer.
 
        .PARAMETER Width
            The width of the object in the workflow designer.
 
        .PARAMETER X
            The x location of the object in the workflow designer.
 
        .PARAMETER Y
            The y location of the object in the workflow designer.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            Workflow
            WorkflowItem
 
        .EXAMPLE
            # Change the label on an item in a workflow
            Get-BPAWorkflow "Some Workflow" | Set-BPAWorkflowItem -ID "{1103992f-cbbd-44fd-9177-9de31b1070ab}" -Label "Do something" -UseLabel
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 03/09/2018
            Date Modified : 06/07/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$ID,

        [ValidateScript({
            if ($_.TypeName -notin "Workflow","Task","Condition","Process") {
                throw [System.Management.Automation.PSArgumentException]"Construct is not a valid type!"
                $false
            } else {
                $true
            }
        })]
        $Construct,

        [ValidateScript({
            if (($_ -ne "") -and ($null -ne $_) -and ($_.TypeName -notin "Agent","AgentGroup")) {
                throw [System.Management.Automation.PSArgumentException]"Agent is not an Agent type!"
                $false
            } else {
                $true
            }
        })]
        $Agent,

        [ValidateNotNullOrEmpty()]
        [switch]$Enabled,

        [ValidateNotNullOrEmpty()]
        [switch]$UseLabel,

        [ValidateNotNull()]
        [string]$Label,

        [ValidateNotNullOrEmpty()]
        [int]$Height,

        [ValidateNotNullOrEmpty()]
        [int]$Width,

        [ValidateNotNullOrEmpty()]
        [int]$X,

        [ValidateNotNullOrEmpty()]
        [int]$Y,

        [ValidateNotNull()]
        [string]$Expression
    )

    BEGIN {
        if ($null -eq $Agent) { $Agent = "" }
    }

    PROCESS {
        :objectloop foreach ($obj in $InputObject) {
            switch ($obj.Type -as [BPAConstructType]) {
                "Workflow" {
                    $updateObject = Get-BPAWorkflow -ID $obj.ID -BPAServer $obj.BPAServer
                    $item = $updateObject.Items | Where-Object {$_.ID -eq $ID}
                    if ($null -eq $item) {                        
                        $item = $updateObject.Triggers | Where-Object {$_.ID -eq $ID}
                    }
                }
                "WorkflowItem" {
                    $updateObject = Get-BPAObject -ID $obj.WorkflowID -Types Workflow
                    if (($updateObject | Measure-Object).Count -eq 1) {
                        $item = $updateObject.Items | Where-Object {$_.ID -eq $obj.ID}
                        if ($null -eq $item) {                        
                            $item = $updateObject.Triggers | Where-Object {$_.ID -eq $obj.ID}
                        }
                    } else {
                        Write-Warning "Multiple workflows found for ID $($obj.WorkflowID)! No action will be taken."
                        continue objectloop
                    }
                }
                default {                    
                    Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
                }
            }
            if ($null -eq $item) {                        
                throw "Unable to find workflow item with the specified ID!"
                break
            }
            $shouldUpdate = $false
            if ($PSBoundParameters.ContainsKey("Construct") -and ($item.ConstructID -ne $Construct.ID)) {
                $item.ConstructID = $Construct.ID
                $item.ConstructType = $Construct.Type
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Agent") -and ($item.AgentID -ne $Agent.ID)) {
                $item.AgentID = $Agent.ID
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Enabled") -and ($item.Enabled -ne $Enabled.ToBool())) {
                $item.Enabled = $Enabled.ToBool()
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("UseLabel") -and ($itemvar.UseLabel -ne $UseLabel.ToBool())) {
                $item.UseLabel = $UseLabel.ToBool()
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Label") -and ($item.Label -ne $Label)) {
                $item.Label = $Label
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Height") -and ($item.Height -ne $Height)) {
                $item.Height = $Height
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Width") -and ($item.Width -ne $Width)) {
                $item.Width = $Width
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("X") -and ($item.X -ne $X)) {
                $item.X = $X
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Y") -and ($item.Y -ne $Y)) {
                $item.Y = $Y
                $shouldUpdate = $true
            }
            if ($PSBoundParameters.ContainsKey("Expression") -and ($item.Expression -ne $Expression) -and ($item.ConstructType -eq [BPAConstructType]::Evaluation.value__)) {
                $item.Expression = $Expression
                $shouldUpdate = $true
            }
            if ($shouldUpdate) {
                $updateObject | Set-BPAObject
            } else {
                Write-Verbose "No changes will be made to item '$($item.ID)' in $($updateObject.TypeName) '$($updateObject.Name)'."
            }
        }
    }
}