Scripts/Set-BPAProcessCondition.ps1

function Set-BPAProcessCondition {    
    <#
        .SYNOPSIS
            Sets properties of an AutoMate BPA process condition.
 
        .DESCRIPTION
            Set-BPAProcessCondition modifies an existing process condition.
 
        .PARAMETER InputObject
            The condition to modify.
 
        .PARAMETER ProcessName
            The name of the process.
 
        .PARAMETER Action
            The state of the process to trigger on.
 
        .PARAMETER TriggerOnce
            Only trigger the first time the process is started or stopped.
 
        .PARAMETER Started
            Only trigger when the process was running prior to the condition and is then stopped.
 
        .PARAMETER Wait
            Wait for the condition, or evaluate immediately.
 
        .PARAMETER Timeout
            If wait is specified, the amount of time before the condition times out.
 
        .PARAMETER TimeoutUnit
            The unit for Timeout (Seconds by default).
 
        .PARAMETER TriggerAfter
            The number of times the condition should occur before the trigger fires.
 
        .PARAMETER Notes
            The new notes to set on the object.
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 02/05/2018
            Date Modified : 02/23/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [string]$ProcessName,
        [BPAProcessTriggerState]$Action = [BPAProcessTriggerState]::StoppedResponding,
        [switch]$TriggerOnce = $false,
        [switch]$Started = $false,

        [switch]$Wait = $true,
        [int]$Timeout = 0,
        [BPATimeMeasure]$TimeoutUnit = [BPATimeMeasure]::Seconds,
        [int]$TriggerAfter = 1,

        [string]$Notes = ""
    )

    PROCESS {
        foreach ($obj in $InputObject) {
            if ($obj.TypeName -eq "Condition" -and $obj.TriggerType -eq [BPATriggerType]::Process.value__) {
                $updateObject = Get-BPACondition -ID $obj.ID -BPAServer $obj.BPAServer
                $shouldUpdate = $false
                $boundParameterKeys = $PSBoundParameters.Keys | Where-Object {($_ -notin "InputObject","BPAServer") -and `
                                                                              ($_ -notin [System.Management.Automation.PSCmdlet]::CommonParameters) -and `
                                                                              ($_ -notin [System.Management.Automation.PSCmdlet]::OptionalCommonParameters)}
                foreach ($boundParameterKey in $boundParameterKeys) {
                    $property = $boundParameterKey
                    $value = $PSBoundParameters[$property]

                    # Handle special property types
                    if ($value -is [System.Management.Automation.SwitchParameter]) { $value = $value.ToBool() }
                    elseif ($value -is [System.Enum])                              { $value = $value.value__  }

                    # Compare and change properties
                    if ($updateObject."$property" -ne $value) {
                        $updateObject."$property" = $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)' and trigger type '$($obj.TriggerType -as [BPATriggerType])' encountered!" -TargetObject $obj
            }
        }
    }
}