functions/github/Assert-GitHubTeamMembers.ps1

function Assert-GitHubTeamMembers
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, ParameterSetName="ByName")]
        [string] $Org,

        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="ByObject")]
        $Team,

        [Parameter(Mandatory=$true, ParameterSetName="ByName")]
        [string] $TeamName,

        [Parameter(Mandatory=$true)]
        [AllowEmptyCollection()]
        [string[]] $Members,

        [switch] $RemoveExtraMembers
    )

    if ($PSCmdlet.ParameterSetName -eq "ByObject") {
        $Org = $Team.organization.login
        $TeamName = $Team.slug
    }

    # Setup the results object
    $teamMemberResults = @{
        team_name = $TeamName
        members_added = @()
        members_removed = @()
    }

    Write-Information "Processing team membership for '$TeamName'"

    $existingMembers = Invoke-GitHubRestMethod -Uri "https://api.github.com/orgs/$Org/teams/$TeamName/members"

    $membersToAdd = $Members | ? { $_ -notin $existingMembers.login }
    foreach ($memberToAdd in $membersToAdd) {
        Write-Information "Adding user '$memberToAdd' to team '$TeamName'"
        $resp = Invoke-GitHubRestMethod `
                            -Uri "https://api.github.com/orgs/$Org/teams/$TeamName/memberships/$memberToAdd" `
                            -Verb PUT `
                            -Body @{
                                role = "member"
                            }
        $teamMemberResults.members_added += $memberToAdd
    }

    if ($RemoveExtraMembers) {
        $membersToRemove = $existingMembers.login | ? { $_ -notin $Members }
        foreach ($memberToRemove in $membersToRemove) {
            Write-Information "Removing user '$memberToRemove' from team '$TeamName'"
            $resp = Invoke-GitHubRestMethod `
                                -Uri "https://api.github.com/orgs/$Org/teams/$TeamName/memberships/$memberToRemove" `
                                -Verb DELETE
            $teamMemberResults.members_removed += $memberToRemove
        }
    }

    return $teamMemberResults
}