Identity/Group/Identity_Group.ps1

Function Add-TfsGroupMember
{
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
    Param
    (
        # Specifies the board name(s). Wildcards accepted
        [Parameter(Position=0)]
        [Alias('Name')]
        [Alias('Member')]
        [Alias('User')]
        [object]
        $Identity,

        [Parameter(ValueFromPipeline=$true)]
        [object]
        $Group,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."}

        $gi = Get-TfsIdentity -Identity $Group -Collection $tpc
        $ui = Get-TfsIdentity -Identity $Identity -Collection $tpc

        if(-not $gi)
        {
            throw "Invalid or non-existent group '$Group'"
        }

        if(-not $ui)
        {
            throw "Invalid or non-existent identity '$Identity'"
        }

        $client = _GetRestClient 'Microsoft.VisualStudio.Services.Identity.Client.IdentityHttpClient' -Collection $tpc

        _Log "Adding $($ui.IdentityType) '$($ui.DisplayName) ($($ui.Properties['Account']))' to group '$($gi.DisplayName)'"

        if(-not $PSCmdlet.ShouldProcess($gi.DisplayName, "Add member '$($ui.DisplayName) ($($ui.Properties['Account']))'"))
        {
            return
        }

        $task = $client.AddMemberToGroupAsync($gi.Descriptor, $ui.Descriptor); $result = $task.Result; if($task.IsFaulted) { _throw  "Error adding member '$($ui.DisplayName)' to group '$($gi.DisplayName)'" $task.Exception.InnerExceptions }
    }
}
Function Get-TfsGroupMember
{
    [CmdletBinding()]
    [OutputType('Microsoft.VisualStudio.Services.Identity.Identity')]
    Param
    (
        # Specifies the board name(s). Wildcards accepted
        [Parameter(Position=0)]
        [SupportsWildcards()]
        [object]
        $Identity = '*',

        [Parameter(ValueFromPipeline=$true)]
        [object]
        $Group,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."}

        _Log "Returning members from group '$Group'"

        $gi = Get-TfsIdentity -Identity $Group -Collection $tpc -QueryMembership

        if(-not $gi)
        {
            throw "Invalid or non-existent group '$Group'"
        }

        foreach($mid in $gi.MemberIds)
        {
            $i = Get-TfsIdentity -Identity $mid -Collection $Collection

            if (($i.DisplayName -like $Identity) -or ($i.Properties['Account'] -like $Identity))
            {
                Write-Output $i
            }
        }
    }
}
Function Remove-TfsGroupMember
{
    [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
    Param
    (
        # Specifies the board name(s). Wildcards accepted
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [Alias('Name')]
        [Alias('User')]
        [Alias('Member')]
        [object]
        $Identity,

        [Parameter()]
        [object]
        $Group,

        [Parameter()]
        [object]
        $Project,

        [Parameter()]
        [object]
        $Collection
    )

    Process
    {
        $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."}

        $gi = Get-TfsIdentity -Identity $Group -Collection $tpc
        $ui = Get-TfsIdentity -Identity $Identity -Collection $tpc

        if(-not $gi)
        {
            throw "Invalid or non-existent group '$Group'"
        }

        if(-not $ui)
        {
            throw "Invalid or non-existent identity '$Identity'"
        }

        $client = _GetRestClient 'Microsoft.VisualStudio.Services.Identity.Client.IdentityHttpClient' -Collection $tpc

        _Log "Removing $($ui.IdentityType) '$($ui.DisplayName) ($($ui.Properties['Account']))' from group '$($gi.DisplayName)'"

        if(-not $PSCmdlet.ShouldProcess($gi.DisplayName, "Remove member '$($ui.DisplayName) ($($ui.Properties['Account']))'"))
        {
            return
        }

        $task = $client.RemoveMemberFromGroupAsync($gi.Descriptor, $ui.Descriptor); $result = $task.Result; if($task.IsFaulted) { _throw  "Error removing member '$($ui.DisplayName)' from group '$($gi.DisplayName)'" $task.Exception.InnerExceptions }
    }
}