Functions/Add-GSuiteGroupMember.ps1

<#
.SYNOPSIS
    This function adds a member to a group in GSuite.
#>

function Add-GSuiteGroupMember {
    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([PSCustomObject])]
    param (
        # The primary email address of the group.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$groupPrimaryEmailAddress,

        # The primary email address of the member.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$memberPrimaryEmailAddress
    )

    # Validate that the 'connection' has been established
    if (!$Global:GSuiteAccessTokensHashTable) {
        throw "You must call the Connect-GSuiteAdminAccount cmdlet before calling any other GSuite cmdlets."
    }

    # Validate that the group access token exists in the hash table
    if ([String]::IsNullOrWhiteSpace($Global:GSuiteAccessTokensHashTable.Group)) {
        throw "Group access token is required to call Add-GSuiteGroupMember."
    }

    # Prepare REST call parameters
    $invokeRestMethodParams = @{
        Uri     = "https://www.googleapis.com/admin/directory/v1/groups/$($groupPrimaryEmailAddress)/members"
        Method  = "POST"
        Headers = @{
            "Content-Type" = "application/json"
            Accept         = "application/json"
            Authorization  = "Bearer $($Global:GSuiteAccessTokensHashTable.Group)"
        }
        Body    = @{
            email = $memberPrimaryEmailAddress
        } | ConvertTo-Json
    }

    # Invoke the REST call
    return Invoke-RestMethod @invokeRestMethodParams
}