Scripts/New-BPAScheduleCondition.ps1

function New-BPAScheduleCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA schedule condition.
 
        .DESCRIPTION
            New-BPAScheduleCondition creates a new schedule condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER ScheduleType
            Set the schedule to use a Custom interval.
 
        .PARAMETER NextLaunchDate
            The next time the schedule will execute
 
        .PARAMETER Frequency
            How frequently the schedule should execute the specified interval. For example: every 1 day, every 2 days, etc.
 
        .PARAMETER Day
            The day(s) of the week to execute the schedule on.
 
        .PARAMETER End
            The last date the schedule will execute.
 
        .PARAMETER Measure
            When using the -MonthInterval option, use this parameter to specify regular days, work days, or a specific weekday (i.e. Monday, Tuesday, etc.).
 
        .PARAMETER Month
            The month(s) to execute.
 
        .PARAMETER MonthInterval
            The frequency to execute when using a "Specific day(s) of the month" schedule type.
 
        .PARAMETER OnTaskLate
            The action to take when the task is late.
 
        .PARAMETER Reschedule
            Specify how the schedule should be rescheduled.
 
        .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.
 
        .EXAMPLE
            # Create a new schedule to run every day at 8AM
            New-BPAScheduleCondition -Name "Daily at 8AM" -DayInterval -NextLaunchDate (Get-Date "8:00AM")
 
        .EXAMPLE
            # Create a new schedule to run every other month on the third Wednesday at 9PM
            New-BPAScheduleCondition -Name "Third Wednesday" -MonthInterval -Measure Wednesday -DayOfMonth third -Frequency 2 -NextLaunchDate (Get-Date "9:00PM")
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 07/10/2017
            Date Modified : 03/27/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [ValidateNotNullOrEmpty()]
        [DateTime]$NextLaunchDate = (Get-Date),

        [ValidateSet(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,"first","second","third","fourth","last")]
        $Frequency = 1,

        [DayOfWeek[]]$Day,

        [DateTime]$End,

        [ValidateNotNullOrEmpty()]
        [BPAScheduleMeasure]$Measure = [BPAScheduleMeasure]::Day,

        [ValidateSet("January","February","March","April","May","June","July","August","September","October","November","December")]
        [string[]]$Month,

        [ValidateNotNullOrEmpty()]
        [int]$MonthInterval,

        [ValidateNotNullOrEmpty()]
        [BPAOnTaskLateRescheduleOption]$OnTaskLate = [BPAOnTaskLateRescheduleOption]::RunImmediately,

        [ValidateNotNullOrEmpty()]
        [BPARescheduleOption]$Reschedule = [BPARescheduleOption]::RelativeToOriginalTime,

        [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 task 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]::Schedule) -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.ScheduleType   = $ScheduleType.value__
    $newObject.Frequency      = $Frequency
    $newObject.OnTaskLate     = $OnTaskLate.value__
    $newObject.Reschedule     = $Reschedule.value__
    $newObject.Measure        = $Measure.value__
    if ($PSBoundParameters.ContainsKey("Day") -and ($Day | Measure-Object).Count -gt 0) {
        $newObject.Day = ($Day -join ",").ToLower() -split ","
    }
    if ($PSBoundParameters.ContainsKey("NextLaunchDate")) {
        $newObject.NextLaunchDate = Get-Date $NextLaunchDate -Format $BPAScheduleDateFormat
    }
    if ($PSBoundParameters.ContainsKey("End")) {
        $newObject.End = Get-Date $End -Format $BPAScheduleDateFormat
    }
    if ($ScheduleType -eq [BPAScheduleType]::MonthInterval) {
        $newObject.MonthInterval = $MonthInterval
    }
    if (($PSBoundParameters.ContainsKey("Month")) -and ($Month.Count -gt 0)) {
        $newObject.Month += $Month.ToLower()
    }
    $newObject | New-BPAObject -BPAServer $BPAServer
    return (Get-BPACondition -ID $guid -BPAServer $BPAServer)
}