Scripts/New-BPAProcess.ps1

function New-BPAProcess {
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA process.
 
        .DESCRIPTION
            New-BPAProcess creates a new process object.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER Notes
            The new notes to set on the object.
 
        .PARAMETER CommandLine
            The command to run.
 
        .PARAMETER WorkingDirectory
            The working directory to use when running the command.
 
        .PARAMETER EnvironmentVariables
            Environment variables to load when running the command.
 
        .PARAMETER RunningContext
            The context to execute the command in: Default, SH or Bash.
 
        .PARAMETER Folder
            The folder to place the object in.
 
        .PARAMETER BPAServer
            The server to create the object on.
 
        .EXAMPLE
            # Change command for a process
            New-BPAProcess -Name "Test Process" -CommandLine "dir.exe > %TEMP%\dir.txt" -EnvironmentVariables "TEMP"
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 11/07/2016
            Date Modified : 02/06/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [string]$Notes = "",

        [ValidateNotNullOrEmpty()]
        [string]$CommandLine,

        [string]$WorkingDirectory = "",

        [string]$EnvironmentVariables = "",

        [BPAProcessRunningContext]$RunningContext = ([BPAProcessRunningContext]::Default),

        [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 process 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 PROCESSES -BPAServer $BPAServer
    }

    # Get the template object from the PoshBPA\ObjectTemplates folder, and configure the object
    $newObject = Get-BPAObjectTemplate -Type "Process" -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.CommandLine          = $CommandLine
    $newObject.EnvironmentVariables = $EnvironmentVariables
    $newObject.RunProcessAs         = $RunningContext
    $newObject.WorkingDirectory     = $WorkingDirectory

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