Scripts/New-BPAProcessCondition.ps1

function New-BPAProcessCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA process condition.
 
        .DESCRIPTION
            New-BPAProcessCondition creates a new process condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .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.
 
        .PARAMETER Folder
            The folder to place the object in.
 
        .PARAMETER BPAServer
            The server to create the object on.
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 02/05/2018
            Date Modified : 02/23/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory = $true)]
        [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 = "",

        [ValidateScript({$_.TypeName -eq "Folder"})]
        $Folder,

        [string]$BPAServer
    )

    $guid = "{$((New-Guid).Guid)}"

    if (-not $BPAServer -and $global:BPAConnectionInfo.Count -gt 1) {
        throw "Multiple BPA Servers are connected, please specify which server to create a new condition on!"
    } elseif (-not $BPAServer) {
        $BPAServer = $BPAConnectionInfo.Server
    }

    $user = Get-BPAUser -BPAServer $BPAServer | Where-Object {$_.Name -ieq ($BPAConnectionInfo | Where-Object {$_.Server -eq $BPAServer}).Credential.UserName}
    if (-not $Folder) {
        # Place the task in the users condition folder
        $Folder = $user | Get-BPAFolder -Type CONDITIONS
    }

    # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
    $newObject = Get-BPAObjectTemplate -ConditionType ([BPATriggerType]::Process) -BPAServer $BPAServer
    $newObject.ID             = $guid
    $newObject.Name           = $Name
    $newObject.ParentID       = $Folder.ID
    $newObject.Path           = Join-Path -Path $Folder.Path -ChildPath $Folder.Name
    $newObject.CreatedBy      = $user.ID
    $newObject.Notes          = $Notes
    $newObject.Wait           = $Wait.ToBool()
    if ($newObject.Wait) {
        $newObject.Timeout                 = $Timeout
        $newObject.TimeoutUnit             = $TimeoutUnit.value__
        $newObject.TriggerAfter            = $TriggerAfter
    }
    $newObject.Action         = $Action.value__
    $newObject.ProcessName    = $ProcessName
    if ($Action -in ([BPAProcessTriggerState]::Started,[BPAProcessTriggerState]::Ended)) {
        $newObject.TriggerOnce = $TriggerOnce.ToBool()
    }
    if ($Action -eq [BPAProcessTriggerState]::Ended) {
        $newObject.Started = $Started.ToBool()
    }

    $newObject | New-BPAObject -BPAServer $BPAServer
    return (Get-BPACondition -ID $guid -BPAServer $BPAServer)
}