Scripts/Set-BPATask.ps1

function Set-BPATask {
    <#
        .SYNOPSIS
            Sets properties of an AutoMate BPA task.
 
        .DESCRIPTION
            Set-BPATask can change properties of a task object.
 
        .PARAMETER InputObject
            The object to modify.
 
        .PARAMETER Notes
            The new notes to set on the object.
 
        .PARAMETER AML
            The new AutoMate Markup Language (AML) 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:
            Task
 
        .EXAMPLE
            # Change notes for a task
            Get-BPATask "Delete Log Files" | Set-BPATask -Notes "Deletes old log files"
 
        .EXAMPLE
            # Change AML for a task
            Get-BPATask "Some Task" | Set-BPATask -AML (Get-BPATask "Some Other Task").AML
 
        .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 "Task") {
                $true
            } else {
                throw [System.Management.Automation.PSArgumentException]"Instance is not a Task!"
            }
        })]
        $Instance,

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

        [Parameter(ParameterSetName = "ByInputObject")]
        [ValidateScript({
            if (($_ -is [string]) -and ($_.Trim() -like "<AMTASK>*</AMTASK>")) {
                $true
            } else {
                throw [System.Management.Automation.PSArgumentException]"AML is invalid!"
            }
        })]
        [string]$AML,

        [Parameter(ParameterSetName = "ByInputObject")]
        [BPACompletionState]$CompletionState
    )
    PROCESS {
        switch ($PSCmdlet.ParameterSetName) {
            "ByInputObject" {
                foreach ($obj in $InputObject) {
                    if ($obj.TypeName -eq "Task") {
                        $updateObject = Get-BPATask -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("AML")) {
                            if ($updateObject.AML -ne $AML) {
                                $updateObject.AML = $AML
                                $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.AML -is [string]) -and ($updateObject.AML.Trim() -notlike "<AMTASK>*</AMTASK>")) {
                    $validated = $false
                    throw "$($Instance.TypeName) '$($Instance.Name)' has invalid AML!"
                }
                if ($validated) {
                    $updateObject | Set-BPAObject
                }
            }
        }
    }
}