Scripts/New-BPAServiceCondition.ps1

function New-BPAServiceCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA service condition.
 
        .DESCRIPTION
            New-BPAServiceCondition creates a new service condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER ServiceName
            The name of the service.
 
        .PARAMETER Action
            The state of the service to trigger on.
 
        .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]$ServiceName,

        [BPAServiceTriggerState]$Action = [BPAServiceTriggerState]::StoppedResponding,

        [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]::Service) -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.ServiceName    = $ServiceName

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