Scripts/Set-BPACondition.ps1

function Set-BPACondition {
    <#
        .SYNOPSIS
            Sets properties of an AutoMate BPA condition.
 
        .DESCRIPTION
            Set-BPACondition can change properties of a condition object.
 
        .PARAMETER InputObject
            The object to modify.
 
        .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:
            Task
 
        .EXAMPLE
            # Change notes for a task
            Get-BPACondition "Daily at 8AM" | Set-BPACondition -Notes "Starts every day at 8AM"
 
        .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 "Condition") {
                $true
            } else {
                throw [System.Management.Automation.PSArgumentException]"Instance is not a Condition!"
            }
        })]
        $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 "Condition") {
                        $updateObject = Get-BPACondition -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 ($validated) {
                    $updateObject | Set-BPAObject
                }
            }
        }
    }
}