Scripts/New-BPAAgent.ps1

function New-BPAAgent {
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA agent.
 
        .DESCRIPTION
            New-BPAAgent creates a new agent object.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER Type
            The type of agent to create: TaskAgent or ProcessAgent.
 
        .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 new task agent
            New-BPAAgent -Name Task_Agent1 -Type TaskAgent
 
        .EXAMPLE
            # Create new process agent
            New-BPAAgent -Name Proc_Agent1 -Type ProcessAgent
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 11/07/2016
            Date Modified : 02/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [ValidateNotNullOrEmpty()]
        [BPAAgentType]$Type = ([BPAAgentType]::TaskAgent),

        [string]$Notes = "",

        [ValidateScript({$_.TypeName -eq "Folder"})]
        $Folder,

        [string]$BPAServer
    )

    if ($Type -eq [BPAAgentType]::All) {
        throw "Please specify an agent type!"
    }

    $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 agent 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) {        
        switch ($Type) {
            "TaskAgent" {
                $Folder = Get-BPAFolder -Name TASKAGENTS -BPAServer $BPAServer
            }
            "ProcessAgent" {
                $Folder = Get-BPAFolder -Name PROCESSAGENTS -BPAServer $BPAServer
            }
        }
    }

    # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
    $newObject = Get-BPAObjectTemplate -Type "Agent" -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.AgentType   = $Type
    
    $newObject | New-BPAObject -BPAServer $BPAServer
    return (Get-BPAAgent -ID $guid -BPAServer $BPAServer)
}