Team/TeamMember/Team_TeamMember.ps1
Function Add-TfsTeamMember { [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')] [OutputType('TfsCmdlets.TeamAdmins')] Param ( # Specifies the board name(s). Wildcards accepted [Parameter(Position=0)] [Alias('Name')] [Alias('Member')] [Alias('User')] [object] $Identity, [Parameter(ValueFromPipeline=$true)] [object] $Team, [Parameter()] [object] $Project, [Parameter()] [object] $Collection ) Process { $t = Get-TfsTeam -Team $Team -Project $Project -Collection $Collection; if ($t.Count -ne 1) {throw "Invalid or non-existent team '$Team'."}; if($t.ProjectName) {$Project = $t.ProjectName}; $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection $gi = Get-TfsIdentity -Identity $t.Id -Collection $tpc $ui = Get-TfsIdentity -Identity $Identity -Collection $tpc 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 team '$($t.Name)'" if(-not $PSCmdlet.ShouldProcess($t.Name, "Add member '$($ui.DisplayName) ($($ui.Properties['Account']))'")) { return } $task = $client.AddMemberToGroupAsync($gi.Descriptor, $ui.Descriptor); $result = $task.Result; if($task.IsFaulted) { throw "Error adding team member '$($ui.DisplayName)' to team '$($t.Name)'" + ": $($task.Exception.InnerExceptions | ForEach-Object {$_.ToString()})" } } } Function Get-TfsTeamMember { [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] $Team, [Parameter()] [object] $Project, [Parameter()] [object] $Collection ) Process { if($Team -is [Microsoft.TeamFoundation.Core.WebApi.WebApiTeam]) { $Project = $Team.ProjectId } $t = Get-TfsTeam -Team $Team -Project $Project -Collection $Collection -IncludeMembers $tpc = Get-TfsTeamProjectCollection -Collection $Collection; if (-not $tpc -or ($tpc.Count -ne 1)) {throw "Invalid or non-existent team project collection $Collection."} _Log "Returning team members from team '$($t.Name)'" foreach($member in $t.Members) { $i = Get-TfsIdentity -Identity $member.Identity.Id -Collection $Collection if (($i.DisplayName -like $Identity) -or ($i.Properties['Account'] -like $Identity)) { Write-Output $i | ` Add-Member -Name TeamId -MemberType NoteProperty -Value $t.Id -PassThru | ` Add-Member -Name ProjectId -MemberType NoteProperty -Value $t.ProjectId -PassThru } } } } Function Remove-TfsTeamMember { [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] $Team, [Parameter()] [object] $Project, [Parameter()] [object] $Collection ) Process { if($Identity.TeamId -and $Identity.ProjectId) { $Project = $Identity.ProjectId $t = Get-TfsTeam -Team $Identity.TeamId -Project $Project -Collection $Collection $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection } else { $t = Get-TfsTeam -Team $Team -Project $Project -Collection $Collection; if ($t.Count -ne 1) {throw "Invalid or non-existent team '$Team'."}; if($t.ProjectName) {$Project = $t.ProjectName}; $tp = Get-TfsTeamProject -Project $Project -Collection $Collection; if (-not $tp -or ($tp.Count -ne 1)) {throw "Invalid or non-existent team project $Project."}; $tpc = $tp.Store.TeamProjectCollection } $gi = Get-TfsIdentity -Identity $t.Id -Collection $tpc $ui = Get-TfsIdentity -Identity $Identity -Collection $tpc 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 team '$($t.Name)'" if(-not $PSCmdlet.ShouldProcess($t.Name, "Remove member '$($ui.DisplayName) ($($ui.Properties['Account']))'")) { return } $task = $client.RemoveMemberFromGroupAsync($gi.Descriptor, $ui.Descriptor); $result = $task.Result; if($task.IsFaulted) { throw "Error removing team member '$($ui.DisplayName)' from team '$($t.Name)'" + ": $($task.Exception.InnerExceptions | ForEach-Object {$_.ToString()})" } } } |