From ac3186563f78134ad18e8184e0d4b7f231f87ec2 Mon Sep 17 00:00:00 2001 From: Reshmee Auckloo Date: Mon, 28 Oct 2024 19:11:06 +0000 Subject: [PATCH] Add groupSetting to cmdlet Get-PnPMicrosoft365GroupSettings (#4481) * Update GetGroupSettings * Update to get group settings * Add groupsetting parameter to the cmdlet --------- Co-authored-by: Gautam Sheth --- .../Get-PnPMicrosoft365GroupSettings.md | 35 ++++++++- .../Microsoft365GroupSettingsPipeBind.cs | 75 +++++++++++++++++++ .../GetMicrosoft365GroupSettings.cs | 20 ++++- .../Utilities/Microsoft365GroupsUtility.cs | 12 ++- 4 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/Commands/Base/PipeBinds/Microsoft365GroupSettingsPipeBind.cs diff --git a/documentation/Get-PnPMicrosoft365GroupSettings.md b/documentation/Get-PnPMicrosoft365GroupSettings.md index d0790e2b1..2659a5233 100644 --- a/documentation/Get-PnPMicrosoft365GroupSettings.md +++ b/documentation/Get-PnPMicrosoft365GroupSettings.md @@ -20,7 +20,7 @@ Gets a settings of a specific Microsoft 365 Group or a tenant wide Microsoft 365 ## SYNTAX ```powershell -Get-PnPMicrosoft365GroupSettings [-Identity ] +Get-PnPMicrosoft365GroupSettings [-Identity ] [-GroupSetting ] ``` ## DESCRIPTION @@ -43,6 +43,22 @@ Get-PnPMicrosoft365GroupSettings -Identity $groupId Retrieves a specific Microsoft 365 Group setting + +### EXAMPLE 3 +```powershell +Get-PnPMicrosoft365GroupSettings -GroupSetting $groupSettingId +``` + +Retrieves a tenant-wide specific Microsoft 365 Group setting. + + +### EXAMPLE 4 +```powershell +Get-PnPMicrosoft365GroupSettings -Identity $groupId -GroupSetting $groupSettingId +``` + +Retrieves a group-specific Microsoft 365 Group setting + ## PARAMETERS ### -Identity @@ -59,10 +75,25 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -GroupSetting +The Identity of the Microsoft 365 Group Setting + +```yaml +Type: Microsoft365GroupSettingsPipeBind +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ## RELATED LINKS [Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) -[Microsoft Graph documentation](https://learn.microsoft.com/graph/api/groupsetting-get) +[Microsoft Graph documentation get settings](https://learn.microsoft.com/graph/api/groupsetting-get) +[Microsoft Graph documentation list settings](https://learn.microsoft.com/en-gb/graph/api/group-list-setting) diff --git a/src/Commands/Base/PipeBinds/Microsoft365GroupSettingsPipeBind.cs b/src/Commands/Base/PipeBinds/Microsoft365GroupSettingsPipeBind.cs new file mode 100644 index 000000000..81731a5d2 --- /dev/null +++ b/src/Commands/Base/PipeBinds/Microsoft365GroupSettingsPipeBind.cs @@ -0,0 +1,75 @@ +using PnP.PowerShell.Commands.Model; +using PnP.PowerShell.Commands.Utilities; +using System; +using System.Management.Automation; + +namespace PnP.PowerShell.Commands.Base.PipeBinds +{ + public class Microsoft365GroupSettingsPipeBind + { + private readonly Microsoft365GroupSetting _group; + private readonly Guid _groupId; + private readonly string _displayName; + + public Microsoft365GroupSettingsPipeBind() + { + } + + public Microsoft365GroupSettingsPipeBind(Microsoft365GroupSetting group) + { + _group = group; + } + + public Microsoft365GroupSettingsPipeBind(string input) + { + Guid idValue; + if (Guid.TryParse(input, out idValue)) + { + _groupId = idValue; + } + else + { + _displayName = input; + } + } + + public Microsoft365GroupSettingsPipeBind(Guid guid) + { + _groupId = guid; + } + + public Microsoft365GroupSetting Group => _group; + + public string DisplayName => _displayName; + + public Guid GroupId => _groupId; + + public Guid GetGroupSettingId(Cmdlet cmdlet, PnPConnection connection, string accessToken) + { + Guid idValue; + if (Group != null) + { + Guid.TryParse(Group.Id, out idValue); + return idValue; + } + else if (_groupId != Guid.Empty) + { + return _groupId; + } + else if (!string.IsNullOrEmpty(DisplayName)) + { + var groups = ClearOwners.GetGroupSettings(cmdlet, connection, accessToken); + if (groups != null) + { + var group = groups.Value.Find(p => p.DisplayName.Equals(DisplayName)); + if (group != null) + { + Guid.TryParse(group.Id, out idValue); + return idValue; + } + } + } + throw new PSInvalidOperationException("Group not found"); + } + } +} diff --git a/src/Commands/Microsoft365Groups/GetMicrosoft365GroupSettings.cs b/src/Commands/Microsoft365Groups/GetMicrosoft365GroupSettings.cs index 520fd3922..5d0dd6c38 100644 --- a/src/Commands/Microsoft365Groups/GetMicrosoft365GroupSettings.cs +++ b/src/Commands/Microsoft365Groups/GetMicrosoft365GroupSettings.cs @@ -14,15 +14,31 @@ public class GetMicrosoft365GroupSettings : PnPGraphCmdlet [Parameter(Mandatory = false)] public Microsoft365GroupPipeBind Identity; + [Parameter(Mandatory = false)] + public Microsoft365GroupSettingsPipeBind GroupSetting; + protected override void ExecuteCmdlet() { - if (Identity != null) + if (Identity != null && GroupSetting != null) + { + var groupId = Identity.GetGroupId(this, Connection, AccessToken); + var groupSettingId = GroupSetting.GetGroupSettingId(this, Connection, AccessToken); + var groupSettings = ClearOwners.GetGroupSettings(this, Connection, AccessToken, groupSettingId.ToString(), groupId.ToString()); + WriteObject(groupSettings, true); + } + else if (Identity != null && GroupSetting == null) { var groupId = Identity.GetGroupId(this, Connection, AccessToken); var groupSettings = ClearOwners.GetGroupSettings(this, Connection, AccessToken, groupId.ToString()); WriteObject(groupSettings?.Value, true); } - else + else if (Identity == null && GroupSetting != null) + { + var groupSettingId = GroupSetting.GetGroupSettingId(this, Connection, AccessToken); + var groupSettings = ClearOwners.GetGroupTenantSettings(this, Connection, AccessToken, groupSettingId.ToString()); + WriteObject(groupSettings, true); + } + else if(Identity == null && GroupSetting == null) { var groupSettings = ClearOwners.GetGroupSettings(this, Connection, AccessToken); WriteObject(groupSettings?.Value, true); diff --git a/src/Commands/Utilities/Microsoft365GroupsUtility.cs b/src/Commands/Utilities/Microsoft365GroupsUtility.cs index 21f16d1aa..f018bdb13 100644 --- a/src/Commands/Utilities/Microsoft365GroupsUtility.cs +++ b/src/Commands/Utilities/Microsoft365GroupsUtility.cs @@ -713,12 +713,22 @@ internal static Microsoft365GroupSettingValueCollection GetGroupSettings(Cmdlet return result; } + internal static Microsoft365GroupSetting GetGroupTenantSettings(Cmdlet cmdlet, PnPConnection connection, string accessToken, string groupSettingId) + { + var result = GraphHelper.Get(cmdlet, connection, $"v1.0/groupSettings/{groupSettingId}", accessToken, propertyNameCaseInsensitive: true); + return result; + } + internal static Microsoft365GroupSettingValueCollection GetGroupSettings(Cmdlet cmdlet, PnPConnection connection, string accessToken, string groupId) { var result = GraphHelper.Get(cmdlet, connection, $"v1.0/groups/{groupId}/settings", accessToken, propertyNameCaseInsensitive: true); return result; } - + internal static Microsoft365GroupSetting GetGroupSettings(Cmdlet cmdlet, PnPConnection connection, string accessToken, string groupSettingId, string groupId) + { + var result = GraphHelper.Get(cmdlet, connection, $"v1.0/groups/{groupId}/settings/{groupSettingId}", accessToken, propertyNameCaseInsensitive: true); + return result; + } internal static Microsoft365GroupSetting CreateGroupSetting(Cmdlet cmdlet, PnPConnection connection, string accessToken, dynamic groupSettingObject) { var stringContent = new StringContent(JsonSerializer.Serialize(groupSettingObject));