Scripts/Add-BPAAgentGroupMember.ps1

function Add-BPAAgentGroupMember {
    <#
        .SYNOPSIS
            Adds agents to an AutoMate BPA agent group.
 
        .DESCRIPTION
            Add-BPAAgentGroupMember can add agents to an agent group.
 
        .PARAMETER InputObject
            The agent group to modify.
 
        .PARAMETER Agent
            The agent(s) to add to the agent group.
 
        .INPUTS
            The following BPA object types can be modified by this function:
            AgentGroup
 
        .EXAMPLE
            # Add all agents to an agent group
            Get-BPAAgentGroup "All Agents" | Add-BPAAgentGroupMember -Agent *
 
        .EXAMPLE
            # Add an agent to an agent group (using agent object)
            Get-BPAAgentGroup | Add-BPAAgentGroupMember -Agent (Get-BPAAgent "Agent1")
 
        .NOTES
            Author(s): : David Seibel
            Contributor(s) :
            Date Created : 11/07/2016
            Date Modified : 05/01/2018
 
        .LINK
            https://github.com/davidseibel/PoshBPA
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $InputObject,

        [Parameter(Mandatory = $true, Position = 0)]
        $Agent
    )
    PROCESS {
        foreach ($obj in $InputObject) {
            if ($obj.TypeName -eq "AgentGroup") {
                $update = Get-BPAAgentGroup -ID $obj.ID -BPAServer $obj.BPAServer
                $shouldUpdate = $false
                foreach ($a in $Agent) {
                    if ($a.PSObject.Properties.Name -contains "TypeName") {
                        if ($a.TypeName -ne "Agent")  {
                            throw "Unsupported input type '$($a.TypeName)' encountered!"
                        }
                    } elseif ($a -is [string]) {
                        $tempAgent = Get-BPAAgent -Name $a -BPAServer $obj.BPAServer
                        if ($tempAgent) {
                            $a = $tempAgent
                        } else {
                            throw "Agent '$a' not found!"
                        }
                    }
                    if ($a.BPAServer -eq $obj.BPAServer) {
                        if ($update.AgentIDs -notcontains $a.ID) {
                            $update.AgentIDs += $a.ID
                            $shouldUpdate = $true
                            Write-Verbose "Adding agent '$($a.Name)' to agent group '$($obj.Name)'."
                        } else {
                            Write-Verbose "Agent '$($a.Name)' already present in agent group '$($obj.Name)'."
                        }
                    } else {
                        Write-Warning "Agent '$($a.Name)' on server $($a.BPAServer) can not be added to agent group '$($obj.Name)' on server $($obj.BPAServer)."
                    }
                }
                if ($shouldUpdate) {
                    $update | Set-BPAObject
                    Write-Verbose "Completed adding agents to agent group '$($obj.Name)'."
                }
            } else {
                Write-Error -Message "Unsupported input type '$($obj.TypeName)' encountered!" -TargetObject $obj
            }
        }    
    }
}