Functions/Add-MembersToMSPCompleteGroupObject.ps1

<#
.SYNOPSIS
    This function retrieves the members for a MSPComplete group object and stores them in the 'Members' property of the object.
#>

function Add-MembersToMSPCompleteGroupObject {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([PSObject])]
    param (
        # The group object
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [PSObject]$group,

        # Select the stream where the output messages will be directed.
        [Parameter(Mandatory=$false)]
        [ValidateSet("Information", "Warning", "Error", "None")]
        [String]$outputStream = "Error"
    )

    # Set the local default parameter values
    # If this is not done, the local default parameter values are null
    $PSDefaultParameterValues = $Global:PSDefaultParameterValues

    # Retrieve the group members
    try {
        $members = @()
        $memberEndUserIds = (Get-BT_GroupMembership -Ticket $mspc.Ticket -GroupId $group.Id -IsDeleted $false -RetrieveAll).EndUserId
        foreach ($endUserId in $memberEndUserIds) {
            $members += (Get-BT_CustomerEndUser -Ticket $mspc.Ticket -Id $endUserId).PrimaryEmailAddress
        }
    }
    catch {
        Write-OutputMessage "Exception occurred while retrieving the members of group '$($group.Name)'.`r`n$($_.Exception.Message)" -OutputStream $outputStream -ReturnMessage:$false
        return
    }

    # Add the group members to the object
    $group | Add-Member -NotePropertyName "Members" -NotePropertyValue $members -Force

    # Return the updated object
    return $group
}