Scripts/New-BPAEventLogCondition.ps1

function New-BPAEventLogCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA event log condition.
 
        .DESCRIPTION
            New-BPAEventLogCondition creates a new event log condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER LogType
            The type of event log to monitor.
 
        .PARAMETER EventSource
            The source of the event to monitor.
 
        .PARAMETER EventType
            The type of event to monitor.
 
        .PARAMETER EventCategory
            The event category to monitor.
 
        .PARAMETER EventDescription
            Allows a description for the event to optionally be entered. To specify partial matches, use wildcard characters * or ?.
 
        .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/06/2018
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [string]$LogType = "Application",
        [string]$EventSource = "All Sources",
        [BPAEventLogTriggerEventType]$EventType = [BPAEventLogTriggerEventType]::Any,
        [string]$EventCategory = "All Categories",
        [string]$EventDescription = "*",

        [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]::EventLog) -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.EventCategory    = $EventCategory
    $newObject.EventDescription = $EventDescription
    $newObject.EventSource      = $EventSource
    $newObject.EventType        = $EventType.value__
    $newObject.LogType          = $EventLog

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