Skip to content

Commit

Permalink
Feature: Add support for removing multiple users from a Teams team (#…
Browse files Browse the repository at this point in the history
…4650)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Dec 24, 2024
1 parent a891180 commit 2e86810
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
27 changes: 24 additions & 3 deletions documentation/Remove-PnPTeamsUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPTeamsUser.htm

**Required Permissions**

* Microsoft Graph API: Group.ReadWrite.All
* Microsoft Graph API: Group.ReadWrite.All, TeamMember.ReadWrite.All

Removes a user from a team.

Expand Down Expand Up @@ -44,6 +44,13 @@ Remove-PnPTeamsUser -Team MyTeam -User [email protected] -Role Owner

Removes the user [email protected] from the owners of the team, but retains the user as a member.

### EXAMPLE 3
```powershell
Remove-PnPTeamsUser -Team MyTeam -Users "[email protected]","[email protected]","[email protected]"
```

Removes the users [email protected], [email protected] and [email protected] from the team.

## PARAMETERS

### -Force
Expand All @@ -67,7 +74,7 @@ Specify the role of the user you are removing from the team. Accepts "Owner" and
```yaml
Type: String
Parameter Sets: (All)
Parameter Sets: (User)

Required: False
Position: Named
Expand Down Expand Up @@ -95,7 +102,21 @@ Specify the UPN (e.g. [email protected])
```yaml
Type: String
Parameter Sets: (All)
Parameter Sets: (User)

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Users
Specify the list of UPN (e.g. [email protected])
```yaml
Type: String
Parameter Sets: (Users)

Required: True
Position: Named
Expand Down
32 changes: 26 additions & 6 deletions src/Commands/Teams/RemoveTeamsUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@
namespace PnP.PowerShell.Commands.Teams
{
[Cmdlet(VerbsCommon.Remove, "PnPTeamsUser")]
[RequiredApiDelegatedOrApplicationPermissions("graph/Group.Read.All")]
[RequiredApiDelegatedOrApplicationPermissions("graph/Group.ReadWrite.All")]
[RequiredApiDelegatedOrApplicationPermissions("graph/TeamMember.ReadWrite.All")]
public class RemoveTeamsUser : PnPGraphCmdlet
{
[Parameter(Mandatory = true)]
const string ParamSet_ByUser = "By User";
const string ParamSet_ByMultipleUsers = "By Multiple Users";

[Parameter(Mandatory = true, ParameterSetName = ParamSet_ByUser)]
[Parameter(Mandatory = true, ParameterSetName = ParamSet_ByMultipleUsers)]
public TeamsTeamPipeBind Team;

[Parameter(Mandatory = true)]
[Parameter(Mandatory = true, ParameterSetName = ParamSet_ByUser)]
public string User;

[Parameter(Mandatory = false)]
[Parameter(Mandatory = true, ParameterSetName = ParamSet_ByMultipleUsers)]
public string[] Users;

[Parameter(Mandatory = false, ParameterSetName = ParamSet_ByUser)]
public string Role = "Member";

[Parameter(Mandatory = false)]
[Parameter(Mandatory = false, ParameterSetName = ParamSet_ByUser)]
[Parameter(Mandatory = false, ParameterSetName = ParamSet_ByMultipleUsers)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
Expand All @@ -30,9 +40,19 @@ protected override void ExecuteCmdlet()
{
try
{
if (Force || ShouldContinue($"Remove user with UPN {User}?", Properties.Resources.Confirm))
if (ParameterSetName == ParamSet_ByUser)
{
if (Force || ShouldContinue($"Remove user with UPN {User}?", Properties.Resources.Confirm))
{
TeamsUtility.DeleteUser(RequestHelper, groupId, User, Role);
}
}
else
{
TeamsUtility.DeleteUser(RequestHelper, groupId, User, Role);
if (Force || ShouldContinue($"Remove specifed users from the team ?", Properties.Resources.Confirm))
{
TeamsUtility.DeleteUsers(RequestHelper, groupId, Users, Role);
}
}
}
catch (GraphException ex)
Expand Down
20 changes: 20 additions & 0 deletions src/Commands/Utilities/TeamsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,26 @@ public static TeamUser UpdateTeamUserRole(ApiRequestHelper requestHelper, string
return result;
}

public static void DeleteUsers(ApiRequestHelper requestHelper, string groupId, string[] upn, string role)
{
var teamChannelMember = new List<TeamChannelMember>();
if (upn != null && upn.Length > 0)
{
foreach (var user in upn)
{
teamChannelMember.Add(new TeamChannelMember() { Roles = null, UserIdentifier = $"https://{requestHelper.GraphEndPoint}/v1.0/users('{user}')" });
}
if (teamChannelMember.Count > 0)
{
var chunks = BatchUtility.Chunk(teamChannelMember, 200);
foreach (var chunk in chunks.ToList())
{
requestHelper.Post($"v1.0/teams/{groupId}/members/remove", new { values = chunk.ToList() });
}
}
}
}

#endregion

#region Channel
Expand Down

0 comments on commit 2e86810

Please sign in to comment.