Skip to content

Commit

Permalink
Feature #3517: Refactor code to use confirm message (#4456)
Browse files Browse the repository at this point in the history
* Feature #3517: Refactor code to use confirm message

* Update docs for cmdlets

---------

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 19, 2024
1 parent 9be6a75 commit 3f3b6b1
Show file tree
Hide file tree
Showing 30 changed files with 82 additions and 110 deletions.
2 changes: 1 addition & 1 deletion documentation/Remove-PnPAvailableSiteClassification.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -Confirm
### -Force
If provided or set to $true, a confirmation will be asked before the actual remove takes place. If omitted or set to $false, it will remove the site classification(s) without asking for confirmation.
```yaml
Expand Down
18 changes: 1 addition & 17 deletions documentation/Remove-PnPUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,13 @@ Remove the user with e-mail address [email protected] from the User In

### EXAMPLE 4
```powershell
Remove-PnPUser -Identity i:0#.f|membership|[email protected] -Confirm:$false
Remove-PnPUser -Identity i:0#.f|membership|[email protected] -Force:$false
```

Remove the user with LoginName i:0#.f|membership|[email protected] from the User Information List of the current site collection without asking to confirm the removal first

## PARAMETERS

### -Confirm
Specifying the Confirm parameter will allow the confirmation question to be skipped

```yaml
Type: SwitchParameter
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Connection
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.

Expand Down Expand Up @@ -110,8 +96,6 @@ Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Admin/NewSdnProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace PnP.PowerShell.Commands.Admin
{
[Cmdlet(VerbsCommon.New, "PnPSdnProvider", SupportsShouldProcess = true)]
[Cmdlet(VerbsCommon.New, "PnPSdnProvider")]
public class NewSdnProvider : PnPAdminCmdlet
{
[Parameter(Mandatory = true)]
Expand All @@ -15,9 +15,9 @@ public class NewSdnProvider : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
if (ShouldProcess("Adds a new SDN Provider"))
if (ShouldContinue("Add a new SDN Provider", Properties.Resources.Confirm))
{
this.Tenant.AddSdnProvider(Identity, License);
Tenant.AddSdnProvider(Identity, License);
AdminContext.ExecuteQueryRetry();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Admin/RemoveExternalUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void ExecuteCmdlet()
var office365Tenant = new Office365Tenant(AdminContext);

var results = office365Tenant.RemoveExternalUsers(UniqueIDs);
if (this.ShouldProcess(nameof(UniqueIDs), "Remove External Users"))
if (ShouldContinue("Remove External Users ?", Properties.Resources.Confirm))
{
var resultObject = new PSObject();
AdminContext.Load(results);
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Admin/RemoveSdnProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace PnP.PowerShell.Commands.Admin
{
[Cmdlet(VerbsCommon.Remove, "PnPSdnProvider", SupportsShouldProcess = true)]
[Cmdlet(VerbsCommon.Remove, "PnPSdnProvider")]
public class RemoveSdnProvider : PnPAdminCmdlet
{
protected override void ExecuteCmdlet()
{
if (ShouldProcess("Removes a SDN Provider"))
if (ShouldContinue("Removes a SDN Provider", Properties.Resources.Confirm))
{
this.Tenant.RemoveSdnProvider();
Tenant.RemoveSdnProvider();
AdminContext.ExecuteQueryRetry();
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Admin/RepairSite.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Text;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;

namespace PnP.PowerShell.Commands.Admin
{
Expand Down Expand Up @@ -39,7 +39,7 @@ protected override void ExecuteCmdlet()
{
builder.Append($", RuleId {RuleId}");
}
if (this.ShouldProcess(builder.ToString()))
if (ShouldContinue(builder.ToString(), Properties.Resources.Confirm))
{
var result = new PSObject();
result.Properties.Add(new PSNoteProperty("SiteUrl", site.Url));
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Admin/RevokeUserSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

namespace PnP.PowerShell.Commands.Admin
{
[Cmdlet(VerbsSecurity.Revoke, "PnPUserSession", SupportsShouldProcess = true)]
[Cmdlet(VerbsSecurity.Revoke, "PnPUserSession")]
public class RevokeUserSession : PnPAdminCmdlet
{
[Parameter(Mandatory = true)]
[ValidateNotNull]
public string User;
protected override void ExecuteCmdlet()
{
if (ShouldProcess($"Sign out user {User} from all devices"))
if (ShouldContinue($"Sign out user {User} from all devices ?", Properties.Resources.Confirm))
{
var office365Tenant = new Office365Tenant(AdminContext);

Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Admin/SetHomeSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected override void ExecuteCmdlet()

if (Tenant.IsMultipleVivaConnectionsFlightEnabled)
{
if (Force.IsPresent || ShouldContinue("Before you update a home site or Viva Connections experiences, make sure you review the documentation at https://aka.ms/homesites. Continue?", string.Empty))
if (Force || ShouldContinue("Before you update a home site or Viva Connections experiences, make sure you review the documentation at https://aka.ms/homesites.", Properties.Resources.Confirm))
{
IEnumerable<TargetedSiteDetails> enumerable = Tenant.GetTargetedSitesDetails()?.Where((TargetedSiteDetails hs) => !hs.IsVivaBackendSite);
AdminContext.ExecuteQueryRetry();
Expand Down Expand Up @@ -61,9 +61,9 @@ protected override void ExecuteCmdlet()
WriteObject(clientResult.Value);
}
}
else if (Force.IsPresent || ShouldContinue("Before you set a Home site, make sure you review the documentation at https://aka.ms/homesites. Continue?", string.Empty))
else if (Force || ShouldContinue("Before you set a Home site, make sure you review the documentation at https://aka.ms/homesites.", Properties.Resources.Confirm))
{
Tenant.ValidateVivaHomeParameterExists(VivaConnectionsDefaultStart);
Tenant.ValidateVivaHomeParameterExists(VivaConnectionsDefaultStart);
HomeSiteConfigurationParam configuration = null;
if (VivaConnectionsDefaultStart || DraftMode)
{
Expand All @@ -78,7 +78,7 @@ protected override void ExecuteCmdlet()
ClientResult<string> clientResult = Tenant.SetSPHSiteWithConfiguration(HomeSiteUrl, configuration);
AdminContext.ExecuteQueryRetry();
WriteObject(clientResult.Value);
}
}
}

private static bool IsSameSiteUrl(string url1, string url2)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Admin/SetSiteArchiveState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void ExecuteCmdlet()
{
WriteObject("The site and its contents cannot be accessed when a site is archived. The site needs to be reactivated if it needs to be accessed. Archived sites can be reactivated instantaneously, without any additional charges within 7 days of the action. After 7 days, reactivations will be charged as per Microsoft 365 Archive Billing, and will take time.");

if (Force || ShouldProcess(Identity.Url))
if (Force || ShouldContinue(Identity.Url, Properties.Resources.Confirm))
{
spoOperation = Tenant.ArchiveSiteByUrl(Identity.Url);
AdminContext.Load(spoOperation);
Expand All @@ -52,7 +52,7 @@ protected override void ExecuteCmdlet()
case "FullyArchived":
{
WriteWarning("Reactivating a site from \"Archived\" state is a paid operation. It can take upto 24hrs for the site to be reactivated. Performing the operation \"Reactivate Archived site\" on target.");
if (Force || ShouldProcess(Identity.Url))
if (Force || ShouldContinue(Identity.Url, Properties.Resources.Confirm))
{
spoOperation = Tenant.UnarchiveSiteByUrl(Identity.Url);
AdminContext.Load(spoOperation);
Expand All @@ -65,7 +65,7 @@ protected override void ExecuteCmdlet()
{
string resourceString = "Reactivating a site from \"Recently Archived\" state is free. Site will be available as an Active site soon.";
WriteObject(resourceString);
if (Force || ShouldProcess(Identity.Url))
if (Force || ShouldContinue(Identity.Url, Properties.Resources.Confirm))
{
spoOperation = Tenant.UnarchiveSiteByUrl(Identity.Url);
AdminContext.Load(spoOperation);
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Admin/TestSite.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Text;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;

namespace PnP.PowerShell.Commands.Admin
{
Expand All @@ -28,7 +28,7 @@ protected override void ExecuteCmdlet()
siteUrl = Identity.Url;
}

var site = this.Tenant.GetSiteByUrl(siteUrl);
var site = Tenant.GetSiteByUrl(siteUrl);
AdminContext.Load(site);
AdminContext.ExecuteQueryRetry();

Expand All @@ -39,7 +39,7 @@ protected override void ExecuteCmdlet()
{
builder.Append($", RuleId {RuleId}");
}
if (this.ShouldProcess(builder.ToString()))
if (ShouldContinue(builder.ToString(), Properties.Resources.Confirm))
{
var result = new PSObject();
result.Properties.Add(new PSNoteProperty("SiteUrl", site.Url));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ApproveTenantServicePrincipalPermissionRequests : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue($"Approve request {RequestId}?", "Continue"))
if (Force || ShouldContinue($"Approve request {RequestId}?", Properties.Resources.Confirm))
{
var servicePrincipal = new SPOWebAppServicePrincipal(AdminContext);
var request = servicePrincipal.PermissionRequests.GetById(RequestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DenyTenantServicePrincipalPermissionRequests : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue($"Deny request {RequestId}?", "Continue"))
if (Force || ShouldContinue($"Deny request {RequestId}?", Properties.Resources.Confirm))
{
var servicePrincipal = new SPOWebAppServicePrincipal(AdminContext);
var request = servicePrincipal.PermissionRequests.GetById(RequestId);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Apps/DisableTenantServicePrincipal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DisableTenantServicePrincipal : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
if (ShouldContinue("Do you want to disable the Tenant Service Principal?", "Continue?"))
if (ShouldContinue("Do you want to disable the Tenant Service Principal?", Properties.Resources.Confirm))
{
var servicePrincipal = new SPOWebAppServicePrincipal(AdminContext);
servicePrincipal.AccountEnabled = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Apps/EnableTenantServicePrincipal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class EnableTenantServicePrincipal : PnPAdminCmdlet

protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue("Do you want to enable the Tenant Service Principal?", "Continue?"))
if (Force || ShouldContinue("Do you want to enable the Tenant Service Principal?", Properties.Resources.Confirm))
{
var servicePrincipal = new SPOWebAppServicePrincipal(AdminContext);
servicePrincipal.AccountEnabled = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override void ExecuteCmdlet()
var results = GraphHelper.Get<RestResultCollection<ServicePrincipal>>(this, Connection, $"/v1.0/servicePrincipals?$filter=appId eq '{appId}'&$select=id", AccessToken);
if (results.Items.Any())
{
if (Force || ShouldContinue($"Revoke permission {Scope}?", "Continue"))
if (Force || ShouldContinue($"Revoke permission {Scope}?", Properties.Resources.Confirm))
{
var servicePrincipal = results.Items.First();
spoWebAppServicePrincipal.GrantManager.Remove(servicePrincipal.Id, Resource, Scope);
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/AzureAD/RemoveAzureADApp.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Management.Automation;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.AzureAD
{
Expand All @@ -20,7 +20,7 @@ protected override void ExecuteCmdlet()
{
var app = Identity.GetApp(this, Connection, AccessToken);

if (Force || ShouldContinue($"Remove app '{app.DisplayName}' with id '{app.Id}'", string.Empty))
if (Force || ShouldContinue($"Remove app '{app.DisplayName}' with id '{app.Id}'", Properties.Resources.Confirm))
{
Utilities.REST.GraphHelper.Delete(this, Connection, $"/v1.0/applications/{app.Id}", AccessToken);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/Base/InvokeBatch.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using PnP.PowerShell.Commands.Model;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

using PnP.Core.Services;
using PnP.PowerShell.Commands.Model;

namespace PnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsLifecycle.Invoke, "PnPBatch", DefaultParameterSetName = PARAMETERSET_Default)]
Expand Down Expand Up @@ -35,7 +33,7 @@ protected override void ExecuteCmdlet()
bool batchExecuted = Batch.Executed;
if (batchExecuted)
{
if (Force || ShouldContinue($"Batch has been invoked before with {Batch.Requests.Count} requests. Invoke again?", "Invoke Batch"))
if (Force || ShouldContinue($"Batch has been invoked before with {Batch.Requests.Count} requests. Invoke again?", Properties.Resources.Confirm))
{
batchExecuted = false;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Commands/Events/RemoveEventReceiver.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Collections.Generic;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Events
{
Expand Down Expand Up @@ -35,8 +35,8 @@ protected override void ExecuteCmdlet()
{
case ParameterSet_LIST:
var list = List.GetList(CurrentWeb);
if(list == null)

if (list == null)
{
throw new PSArgumentException("The provided List could not be found", nameof(List));
}
Expand All @@ -53,8 +53,8 @@ protected override void ExecuteCmdlet()
{
ClientContext.Load(list.EventReceivers);
ClientContext.ExecuteQueryRetry();
foreach(var eventReceiver in list.EventReceivers)

foreach (var eventReceiver in list.EventReceivers)
{
eventReceiversToDelete.Add(eventReceiver);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ protected override void ExecuteCmdlet()
eventReceiversToDelete.AddRange(CurrentWeb.EventReceivers);
eventReceiversToDelete.AddRange(ClientContext.Site.EventReceivers);
}
break;
break;
}
break;
}
Expand All @@ -119,11 +119,11 @@ protected override void ExecuteCmdlet()
return;
}

for(var x = 0; x < eventReceiversToDelete.Count; x++)
for (var x = 0; x < eventReceiversToDelete.Count; x++)
{
var eventReceiver = eventReceiversToDelete[x];

if (Force || (ParameterSpecified("Confirm") && !bool.Parse(MyInvocation.BoundParameters["Confirm"].ToString())) || ShouldContinue(string.Format(Properties.Resources.RemoveEventReceiver, eventReceiver.ReceiverName, eventReceiver.ReceiverId), Properties.Resources.Confirm))
if (Force || ShouldContinue(string.Format(Properties.Resources.RemoveEventReceiver, eventReceiver.ReceiverName, eventReceiver.ReceiverId), Properties.Resources.Confirm))
{
WriteVerbose($"Removing Event Receiver with Id {eventReceiver.ReceiverId} named {eventReceiver.ReceiverName}");
eventReceiver.DeleteObject();
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/Planner/RemovePlannerBucket.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Management.Automation;
using PnP.PowerShell.Commands.Attributes;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Properties;
using PnP.PowerShell.Commands.Utilities;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.Graph
{
Expand Down Expand Up @@ -40,7 +41,7 @@ protected override void ExecuteCmdlet()
var bucket = Identity.GetBucket(this, Connection, AccessToken, planId);
if (bucket != null)
{
if (ShouldProcess($"Remove bucket '{bucket.Name}'"))
if (ShouldContinue($"Remove bucket '{bucket.Name}'", Resources.Confirm))
{
PlannerUtility.RemoveBucket(this, Connection, AccessToken, bucket.Id);
}
Expand All @@ -65,7 +66,7 @@ protected override void ExecuteCmdlet()
var bucket = Identity.GetBucket(this, Connection, AccessToken, BucketId);
if (bucket != null)
{
if (ShouldProcess($"Remove bucket '{bucket.Name}'"))
if (ShouldContinue($"Remove bucket '{bucket.Name}'", Resources.Confirm))
{
PlannerUtility.RemoveBucket(this, Connection, AccessToken, BucketId);
}
Expand Down
Loading

0 comments on commit 3f3b6b1

Please sign in to comment.