Scripts/New-BPASnmpCondition.ps1

function New-BPASnmpCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA SNMP condition.
 
        .DESCRIPTION
            New-BPASnmpCondition creates a new SNMP condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER IPStart
            The starting IP address to filter SNMP requests from. Use "Any" for any IP address (default).
 
        .PARAMETER IPEnd
            The ending IP address to filter SNMP requests from. Use "Any" for any IP address (default).
 
        .PARAMETER Community
            Specifies whether the condition will start the task when a trap is received from a device from any community (default) or only devices within a specific community.
 
        .PARAMETER Enterprise
            Specifies whether the condition will start the task when a trap is received from any Enterprise OID device (default) or only devices within a specific Enterprise OID.
 
        .PARAMETER GenericType
            Specifies that the condition will filter out traps that are not intended for a specific generic type and act on traps received only from a specific generic type.
 
        .PARAMETER OIDStringNotation
            If specified, IODs will be entered as string notations.
 
        .PARAMETER TimetickStringNotation
            If specified, timetick values will be entered as string notations.
 
        .PARAMETER AcceptUnathenticatedTrap
            If specified, version 3 traps will be accepted.
 
        .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/12/2018
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [string]$IPStart = "Any",
        [string]$IPEnd = "Any",
        [string]$Community = "Any",
        [string]$Enterprise = "Any",
        [BPASnmpGenericType]$GenericType = [BPASnmpGenericType]::Any,
        [switch]$OIDStringNotation = $false,
        [switch]$TimetickStringNotation = $false,
        [switch]$AcceptUnathenticatedTrap = $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]::SNMPTrap) -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.IPStart                  = $IPStart
    $newObject.IPEnd                    = $IPEnd
    $newObject.Community                = $Community
    $newObject.Enterprise               = $Enterprise
    $newObject.GenericType              = $GenericType.value__
    $newObject.OIDStringNotation        = $OIDStringNotation.ToBool()
    $newObject.TimetickStringNotation   = $TimetickStringNotation.ToBool()
    $newObject.AcceptUnathenticatedTrap = $AcceptUnathenticatedTrap.ToBool()

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