Scripts/New-BPADatabaseCondition.ps1

function New-BPADatabaseCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA database condition.
 
        .DESCRIPTION
            New-BPADatabaseCondition creates a new database condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER DatabaseType
            The type of database to monitor: SQL (Microsoft) or Oracle.
 
        .PARAMETER Server
            The name of the database server to be monitored.
 
        .PARAMETER NotificationPort
            Indicates the port number that the notification listener listens on for database notifications. If the port number is set to -1 (default), a random port number is assigned to the listener when started.
 
        .PARAMETER Database
            The name of the database to be monitored.
 
        .PARAMETER Table
            The database table which holds the data element(s) to be monitored. This value must include the schema name and table name separated by a dot (.) entered in the following format (minus the brackets): [Schema_Name].[Table_Name].
 
        .PARAMETER UserName
            The username used to authenticate with the database.
 
        .PARAMETER Password
            The password for the specified user.
 
        .PARAMETER Insert
            If set, the condition evaluates to TRUE when an INSERT is performed on the specified database.
 
        .PARAMETER Delete
            If set, the condition evaluates to TRUE when an DELETE is performed on the specified database.
 
        .PARAMETER Update
            If set, the condition evaluates to TRUE when an UPDATE is performed on the specified database.
 
        .PARAMETER Drop
            If set, the condition evaluates to TRUE when an DROP is performed on the specified database.
 
        .PARAMETER Alter
            If set, the condition evaluates to TRUE when an ALTER is performed on the specified database.
 
        .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/08/2018
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [BPADatabaseTriggerType]$DatabaseType = [BPADatabaseTriggerType]::SQL,
        [string]$Server,
        [int]$NotificationPort = -1,
        [string]$Database,
        [string]$Table,
        [string]$UserName,
        #[string]$Password,
        [switch]$Insert,
        [switch]$Delete,
        [switch]$Update,
        [switch]$Drop,
        [switch]$Alter,

        [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
    }

    if ($DatabaseType -ne [BPADatabaseTriggerType]::Oracle) { $NotificationPort = -1 } # Notification port is an Oracle only setting

    # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
    $newObject = Get-BPAObjectTemplate -ConditionType ([BPATriggerType]::Database) -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.DatabaseType     = $DatabaseType.value__
    $newObject.Server           = $Server
    $newObject.NotificationPort = $NotificationPort
    $newObject.Database         = $Database
    $newObject.Table            = $Table
    $newObject.Username         = $UserName
    #$newObject.Password = $Password
    $newObject.Insert           = $Insert.ToBool()
    $newObject.Delete           = $Delete.ToBool()
    $newObject.Update           = $Update.ToBool()
    $newObject.Drop             = $Drop.ToBool()
    $newObject.Alter            = $Alter.ToBool()

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