Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-Microsoft365Group: Add parameters and completer #2268

Merged
merged 1 commit into from
Aug 24, 2022
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
29 changes: 25 additions & 4 deletions src/Commands/Microsoft365Groups/NewMicrosoft365Group.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Enums;
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;

namespace PnP.PowerShell.Commands.Microsoft365Groups
{
Expand All @@ -23,12 +28,22 @@ public class NewPnPMicrosoft365Group : PnPGraphCmdlet
[Parameter(Mandatory = true)]
public String MailNickname;

[Parameter(Mandatory = false)]
public bool MailEnabled = true;

[Parameter(Mandatory = false)]
public String[] Owners;

[Parameter(Mandatory = false)]
public String[] Members;

[Parameter(Mandatory = false)]
[ArgumentCompleter(typeof(EnumAsStringArgumentCompleter<Framework.Enums.Office365Geography>))]
public string PreferredDataLocation;

[Parameter(Mandatory = false)]
public string PreferredLanguage;

[Parameter(Mandatory = false)]
public SwitchParameter IsPrivate;

Expand All @@ -49,11 +64,15 @@ public class NewPnPMicrosoft365Group : PnPGraphCmdlet
public SwitchParameter Force;

[Parameter(Mandatory = false)]
[ArgumentCompleter(typeof(EnumAsStringArgumentCompleter<TeamResourceBehaviorOptions>))]
public TeamResourceBehaviorOptions?[] ResourceBehaviorOptions;

[Parameter(Mandatory = false)]
public Guid[] SensitivityLabels;

[Parameter(Mandatory = false)]
public SwitchParameter SecurityEnabled;

protected override void ExecuteCmdlet()
{
if (MailNickname.Contains(" "))
Expand Down Expand Up @@ -91,9 +110,11 @@ protected override void ExecuteCmdlet()
Description = Description,
MailNickname = MailNickname,
Visibility = IsPrivate ? "Private" : "Public",
MailEnabled = true,
SecurityEnabled = false,
GroupTypes = new string[] { "Unified" }
MailEnabled = MailEnabled,
SecurityEnabled = SecurityEnabled,
GroupTypes = new string[] { "Unified" },
PreferredDataLocation = PreferredDataLocation,
PreferredLanguage = PreferredLanguage,
};

if (ResourceBehaviorOptions != null && ResourceBehaviorOptions.Length > 0)
Expand Down Expand Up @@ -123,7 +144,7 @@ protected override void ExecuteCmdlet()
else
{
WriteWarning("Adding sensitivity labels in App-only context is not supported by Graph API, so it will be skipped in Group creation");
}
}
}

var group = Microsoft365GroupsUtility.CreateAsync(Connection, AccessToken, newGroup, CreateTeam, LogoPath, Owners, Members, HideFromAddressLists, HideFromOutlookClients, Labels).GetAwaiter().GetResult();
Expand Down
26 changes: 26 additions & 0 deletions src/Commands/Utilities/EnumAsStringArgumentCompleter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using PnP.PowerShell.Commands.Utilities;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;

namespace PnP.PowerShell.Commands.Utilities
{
/// <summary>
/// Allow argument completion of enums without prohibiting undefined values.
/// ie. to allow for future extension
/// </summary>
/// <typeparam name="TEnum"></typeparam>
internal sealed class EnumAsStringArgumentCompleter<TEnum> : IArgumentCompleter where TEnum : struct, Enum
{
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
{
return Enum.GetNames(typeof(TEnum))
.Where(x => x.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
.Select(x => new CompletionResult(x, x, CompletionResultType.ParameterValue, x));
}
}
}