Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Added cmdlets for adding/removing/clearing members and owners of M365 Groups #2750

Merged
merged 7 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Added
- Added a `-RowLimit` parameter to `Clear-PnPRecycleBinItem` and `Restore-PnPRecycleBinItem` so that it can be used on recycle bins which hold more than 5000 items [PR #2760](https://github.com/pnp/PnP-PowerShell/pull/2760)
- Added connection option to `Connect-PnPOnline` taking `-Scopes` and `-Credentials` to allow setting up a delegated permission token for use with Microsoft Graph and the Office 365 Management API. See [this wiki page](https://github.com/pnp/PnP-PowerShell/wiki/Connect-options#connect-using-scopes-and-credentials) for more details. [PR #2746](https://github.com/pnp/PnP-PowerShell/pull/2746)
- Added Add-PnPTeamsChannel, Get-PnPTeamsApp, Get-PnPTeamsChannel, Get-PnPTeamsTab, Get-PnPTeamsTeam, Remove-PnPTeamsChannel, Remove-PnPTeamsTab, Remove-PnPTeamsTeam cmdlets
- Added the following cmdlets to add/remove/clear owners and members of Microsoft 365 Groups: `Add-PnPMicrosoft365GroupMember`, `Add-PnPMicrosoft365GroupOwner`, `Remove-PnPMicrosoft365GroupMember`, `Remove-PnPMicrosoft365GroupOwner`, `Clear-PnPMicrosoft365GroupMember`, `Clear-PnPMicrosoft365GroupOwner` [PR #2750](https://github.com/pnp/PnP-PowerShell/pull/2750)
- Added Add-PnPTeamsChannel, Add-PnPTeamsTab, Add-PnPTeamsUser, Get-PnPTeamsApp, Get-PnPTeamsChannel, Get-PnPTeamsChannelMessage, Get-PnPTeamsTab, Get-PnPTeamsTeam, Get-PnPTeamsUser, New-PnPTeamsApp, New-PnPTeamsTeam, Remove-PnPTeamsChannel, Remove-PnPTeamsTab, Remove-PnPTeamsTeam, Remove-PnPTeamsUser, Set-PnPTeamsChannel, Set-PnPTeamsTab, Set-PnPTeamsTeam, Set-PnPTeamsPicture, Submit-PnPTeamsChannelMessage, Update-PnPTeamsApp cmdlets

### Changed
Expand Down
53 changes: 53 additions & 0 deletions Commands/Graph/AddMicrosoft365GroupMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Add, "PnPMicrosoft365GroupMember")]
[Alias("Add-PnPUnifiedGroupMember")]
[CmdletHelp("Adds members to a particular Microsoft 365 Group",
Category = CmdletHelpCategory.Graph,
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Add-PnPMicrosoft365GroupMember -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com""",
Remarks = @"Adds the provided two users as additional members to the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Add-PnPMicrosoft365GroupMember -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com"" -RemoveExisting",
Remarks = @"Sets the provided two users as the only members of the Microsoft 365 Group named ""Project Team"" by removing any current existing members first",
SortOrder = 2)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-post-members")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.None, MicrosoftGraphApiPermission.User_ReadWrite_All | MicrosoftGraphApiPermission.Group_ReadWrite_All)]
public class AddMicrosoft365GroupMember : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to add members to")]
public UnifiedGroupPipeBind Identity;

[Parameter(Mandatory = true, HelpMessage = "The UPN(s) of the user(s) to add to the Microsoft 365 Group as a member")]
public string[] Users;

[Parameter(Mandatory = false, HelpMessage = "If provided, all existing members will be removed and only those provided through Users will become members")]
public SwitchParameter RemoveExisting;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.AddUnifiedGroupMembers(group.GroupId, Users, AccessToken, RemoveExisting.ToBool());
}
}
}
}
#endif
54 changes: 54 additions & 0 deletions Commands/Graph/AddMicrosoft365GroupOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Add, "PnPMicrosoft365GroupOwner")]
[Alias("Add-PnPUnifiedGroupOwner")]
[CmdletHelp("Adds owners to a particular Microsoft 365 Group",
Category = CmdletHelpCategory.Graph,
OutputTypeLink = "https://docs.microsoft.com/graph/api/group-post-owners",
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Add-PnPMicrosoft365GroupOwner -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com""",
Remarks = @"Adds the provided two users as additional owners to the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Add-PnPMicrosoft365GroupOwner -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com"" -RemoveExisting",
Remarks = @"Sets the provided two users as the only owners of the Microsoft 365 Group named ""Project Team"" by removing any current existing owners first",
SortOrder = 2)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-post-owners")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.None, MicrosoftGraphApiPermission.User_ReadWrite_All | MicrosoftGraphApiPermission.Group_ReadWrite_All)]
public class AddMicrosoft365GroupOwner : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to add owners to")]
public UnifiedGroupPipeBind Identity;

[Parameter(Mandatory = true, HelpMessage = "The UPN(s) of the user(s) to add to the Microsoft 365 Group as an owner")]
public string[] Users;

[Parameter(Mandatory = false, HelpMessage = "If provided, all existing owners will be removed and only those provided through Users will become owners")]
public SwitchParameter RemoveExisting;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.AddUnifiedGroupOwners(group.GroupId, Users, AccessToken, RemoveExisting.ToBool());
}
}
}
}
#endif
43 changes: 43 additions & 0 deletions Commands/Graph/ClearMicrosoft365GroupMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Clear, "PnPMicrosoft365GroupMember")]
[Alias("Clear-PnPMicrosoft365GroupMember")]
[CmdletHelp("Removes all current members from a particular Microsoft 365 Group",
Category = CmdletHelpCategory.Graph,
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Clear-PnPMicrosoft365GroupMember -Identity ""Project Team""",
Remarks = @"Removes all the current members from the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-delete-members")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.Group_ReadWrite_All | MicrosoftGraphApiPermission.Directory_ReadWrite_All | MicrosoftGraphApiPermission.GroupMember_ReadWrite_All)]
public class ClearMicrosoft365GroupMember : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to remove all members from")]
public UnifiedGroupPipeBind Identity;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.ClearUnifiedGroupMembers(group.GroupId, AccessToken);
}
}
}
}
#endif
43 changes: 43 additions & 0 deletions Commands/Graph/ClearMicrosoft365GroupOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Clear, "PnPMicrosoft365GroupOwner")]
[Alias("Clear-PnPUnifiedGroupOwner")]
[CmdletHelp("Removes all current owners from a particular Microsoft 365 Group (aka Unified Group)",
Category = CmdletHelpCategory.Graph,
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Clear-PnPMicrosoft365GroupOwner -Identity ""Project Team""",
Remarks = @"Removes all the current owners from the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-delete-owners")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.Group_ReadWrite_All | MicrosoftGraphApiPermission.Directory_ReadWrite_All)]
public class ClearMicrosoft365GroupOwner : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to remove all owners from")]
public UnifiedGroupPipeBind Identity;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.ClearUnifiedGroupOwners(group.GroupId, AccessToken);
}
}
}
}
#endif
4 changes: 1 addition & 3 deletions Commands/Graph/GetUnifiedGroupMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ protected override void ExecuteCmdlet()

if (group != null)
{
// Get members of the group.

// Get members of the group
List<UnifiedGroupUser> members = UnifiedGroupsUtility.GetUnifiedGroupMembers(group, AccessToken);
WriteObject(members);
}

}
}
}
Expand Down
6 changes: 2 additions & 4 deletions Commands/Graph/GetUnifiedGroupOwners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SharePointPnP.PowerShell.Commands.Graph
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.Group_Read_All | MicrosoftGraphApiPermission.User_Read_All | MicrosoftGraphApiPermission.Group_ReadWrite_All | MicrosoftGraphApiPermission.User_ReadWrite_All)]
public class GetUnifiedGroupOwners : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group.")]
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group")]
public UnifiedGroupPipeBind Identity;

protected override void ExecuteCmdlet()
Expand All @@ -39,12 +39,10 @@ protected override void ExecuteCmdlet()

if (group != null)
{
// Get Owners of the group.

// Get Owners of the group
List<UnifiedGroupUser> owners = UnifiedGroupsUtility.GetUnifiedGroupOwners(group, AccessToken);
WriteObject(owners);
}

}
}
}
Expand Down
46 changes: 46 additions & 0 deletions Commands/Graph/RemoveMicrosoft365GroupMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Remove, "PnPMicrosoft365GroupMember")]
[Alias("Remove-PnPUnifiedGroupMember")]
[CmdletHelp("Removes members from a particular Microsoft 365 Group",
Category = CmdletHelpCategory.Graph,
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Remove-PnPMicrosoft365GroupMember -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com""",
Remarks = @"Removes the provided two users as members from the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-delete-members")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.Group_ReadWrite_All | MicrosoftGraphApiPermission.GroupMember_ReadWrite_All | MicrosoftGraphApiPermission.Directory_ReadWrite_All)]
public class RemoveMicrosoft365GroupMember : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to remove members from")]
public UnifiedGroupPipeBind Identity;

[Parameter(Mandatory = true, HelpMessage = "The UPN(s) of the user(s) to remove as members from the Microsoft 365 Group")]
public string[] Users;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.RemoveUnifiedGroupMembers(group.GroupId, Users, AccessToken);
}
}
}
}
#endif
46 changes: 46 additions & 0 deletions Commands/Graph/RemoveMicrosoft365GroupOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Entities;
using OfficeDevPnP.Core.Framework.Graph;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
[Cmdlet(VerbsCommon.Remove, "PnPMicrosoft365GroupOwner")]
[Alias("Remove-PnPUnifiedGroupOwner")]
[CmdletHelp("Removes owners from a particular Microsoft 365 Group",
Category = CmdletHelpCategory.Graph,
SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Remove-PnPMicrosoft365GroupOwner -Identity ""Project Team"" -Users ""john@contoso.onmicrosoft.com"",""jane@contoso.onmicrosoft.com""",
Remarks = @"Removes the provided two users as owners from the Microsoft 365 Group named ""Project Team""",
SortOrder = 1)]
[CmdletRelatedLink(Text = "Documentation", Url = "https://docs.microsoft.com/graph/api/group-delete-owners")]
[CmdletMicrosoftGraphApiPermission(MicrosoftGraphApiPermission.Group_ReadWrite_All | MicrosoftGraphApiPermission.Directory_ReadWrite_All)]
public class RemoveMicrosoft365GroupOwner : PnPGraphCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, HelpMessage = "The Identity of the Microsoft 365 Group to remove owners from")]
public UnifiedGroupPipeBind Identity;

[Parameter(Mandatory = true, HelpMessage = "The UPN(s) of the user(s) to remove as owners from the Microsoft 365 Group")]
public string[] Users;

protected override void ExecuteCmdlet()
{
UnifiedGroupEntity group = null;

if (Identity != null)
{
group = Identity.GetGroup(AccessToken);
}

if (group != null)
{
UnifiedGroupsUtility.RemoveUnifiedGroupOwners(group.GroupId, Users, AccessToken);
}
}
}
}
#endif
10 changes: 5 additions & 5 deletions Commands/Principals/AddUserToGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
namespace SharePointPnP.PowerShell.Commands.Principals
{
[Cmdlet(VerbsCommon.Add, "PnPUserToGroup")]
[CmdletHelp("Adds a user to a group",
[CmdletHelp("Adds a user to a SharePoint group",
Category = CmdletHelpCategory.Principals)]
[CmdletExample(
Code = @"PS:> Add-PnPUserToGroup -LoginName user@company.com -Identity 'Marketing Site Members'",
Remarks = @"Add the specified user to the group ""Marketing Site Members""",
Remarks = @"Add the specified user to the SharePoint group ""Marketing Site Members""",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Add-PnPUserToGroup -LoginName user@company.com -Identity 5",
Remarks = "Add the specified user to the group with Id 5",
Remarks = "Add the specified user to the SharePoint group with Id 5",
SortOrder = 2)]
public class AddUserToGroup : PnPWebCmdlet
{

[Parameter(Mandatory = true, HelpMessage = "The login name of the user", ParameterSetName = "Internal")]
public string LoginName;

[Parameter(Mandatory = true, HelpMessage = "The group id, group name or group object to add the user to.", ValueFromPipeline = true, ParameterSetName = "Internal")]
[Parameter(Mandatory = true, HelpMessage = "The SharePoint group id, SharePoint group name or SharePoint group object to add the user to", ValueFromPipeline = true, ParameterSetName = "Internal")]
#if !ONPREMISES
[Parameter(Mandatory = true, HelpMessage = "The group id, group name or group object to add the user to.", ValueFromPipeline = true, ParameterSetName = "External")]
[Parameter(Mandatory = true, HelpMessage = "The SharePoint group id, SharePoint group name or SharePoint group object to add the user to", ValueFromPipeline = true, ParameterSetName = "External")]
#endif
public GroupPipeBind Identity;

Expand Down
6 changes: 6 additions & 0 deletions Commands/SharePointPnP.PowerShell.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@
<Compile Include="Base\PipeBinds\TeamsTeamPipeBind.cs" />
<Compile Include="Base\PnPConnectedCmdlet.cs" />
<Compile Include="Enums\Office365Workload.cs" />
<Compile Include="Graph\ClearMicrosoft365GroupMember.cs" />
<Compile Include="Graph\ClearMicrosoft365GroupOwner.cs" />
<Compile Include="Graph\RemoveMicrosoft365GroupOwner.cs" />
<Compile Include="Graph\RemoveMicrosoft365GroupMember.cs" />
<Compile Include="Graph\AddMicrosoft365GroupMember.cs" />
<Compile Include="Graph\GetGraphAccessToken.cs" />
<Compile Include="Base\GetAzureCertificate.cs" />
<Compile Include="Base\NewAzureCertificate.cs" />
Expand Down Expand Up @@ -671,6 +676,7 @@
<Compile Include="Files\ResetFileVersion.cs" />
<Compile Include="Graph\GetGraphSubscription.cs" />
<Compile Include="Graph\GetDeletedUnifiedGroup.cs" />
<Compile Include="Graph\AddMicrosoft365GroupOwner.cs" />
<Compile Include="Graph\NewGraphSubscription.cs" />
<Compile Include="Graph\ResetUnifiedGroupExpiration.cs" />
<Compile Include="Graph\GetAADUser.cs" />
Expand Down