Scripts/New-BPAKeyboardCondition.ps1

function New-BPAKeyboardCondition {    
    <#
        .SYNOPSIS
            Creates a new AutoMate BPA keyboard condition.
 
        .DESCRIPTION
            New-BPAKeyboardCondition creates a new keyboard condition.
 
        .PARAMETER Name
            The name of the new object.
 
        .PARAMETER Hotkey
            The hotkey to trigger the condition.
 
        .PARAMETER HotkeyPassthrough
            Allow hotkey to continue to the application.
 
        .PARAMETER Text
            The text to trigger the condition.
 
        .PARAMETER EraseText
            Erase text afterwards.
 
        .PARAMETER Process
            Only run when the specified process is active.
 
        .PARAMETER ProcessFocused
            The process window must be focused.
 
        .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/12/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

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

        [Parameter(ParameterSetName="Hotkey")]
        [string]$Hotkey,

        [Parameter(ParameterSetName="Hotkey")]
        [switch]$HotkeyPassthrough = $false,

        [Parameter(ParameterSetName="Text")]
        [string]$Text,

        [Parameter(ParameterSetName="Text")]
        [switch]$EraseText = $false,

        [string]$Process,
        [switch]$ProcessFocused = $false,

        [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]::Keyboard) -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.Process        = $Process
    $newObject.Foreground     = $ProcessFocused.ToBool()
    switch ($PSCmdlet.ParameterSetName) {
        "Hotkey" {
            $newObject.KeyType = [BPAKeyboardConditionKeyType]::Hotkey.value__
            $newObject.Keys = $Hotkey
            $newObject.PassThrough = $HotkeyPassthrough.ToBool()
        }
        "Text" {
            $newObject.KeyType = [BPAKeyboardConditionKeyType]::Text.value__
            $newObject.Keys = $Text
            $newObject.EraseText = $EraseText.ToBool()
        }
    }

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