|
| 1 | +using Microsoft.Online.SharePoint.TenantAdministration; |
| 2 | +using Microsoft.SharePoint.Client; |
| 3 | +using PnP.PowerShell.Commands.Base; |
| 4 | +using PnP.PowerShell.Commands.Base.PipeBinds; |
| 5 | +using PnP.PowerShell.Commands.Enums; |
| 6 | +using System; |
| 7 | +using System.Management.Automation; |
| 8 | + |
| 9 | +namespace PnP.PowerShell.Commands.Admin |
| 10 | +{ |
| 11 | + [Cmdlet(VerbsCommon.Set, "PnPSiteArchiveState")] |
| 12 | + public class SetSiteArchiveState : PnPAdminCmdlet |
| 13 | + { |
| 14 | + [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] |
| 15 | + public SPOSitePipeBind Identity; |
| 16 | + |
| 17 | + [Parameter(Mandatory = true, Position = 1)] |
| 18 | + public SPOArchiveState ArchiveState; |
| 19 | + |
| 20 | + [Parameter(Mandatory = false)] |
| 21 | + public SwitchParameter NoWait; |
| 22 | + |
| 23 | + [Parameter(Mandatory = false)] |
| 24 | + public SwitchParameter Force; |
| 25 | + |
| 26 | + protected override void ExecuteCmdlet() |
| 27 | + { |
| 28 | + SpoOperation spoOperation = null; |
| 29 | + switch (ArchiveState) |
| 30 | + { |
| 31 | + case SPOArchiveState.Archived: |
| 32 | + { |
| 33 | + 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."); |
| 34 | + |
| 35 | + if (Force || ShouldProcess(Identity.Url)) |
| 36 | + { |
| 37 | + spoOperation = Tenant.ArchiveSiteByUrl(Identity.Url); |
| 38 | + AdminContext.Load(spoOperation); |
| 39 | + AdminContext.ExecuteQueryRetry(); |
| 40 | + } |
| 41 | + |
| 42 | + break; |
| 43 | + } |
| 44 | + case SPOArchiveState.Active: |
| 45 | + { |
| 46 | + SiteProperties sitePropertiesByUrl = Tenant.GetSitePropertiesByUrl(Identity.Url, includeDetail: false); |
| 47 | + AdminContext.Load(sitePropertiesByUrl); |
| 48 | + AdminContext.ExecuteQueryRetry(); |
| 49 | + |
| 50 | + switch (sitePropertiesByUrl.ArchiveStatus) |
| 51 | + { |
| 52 | + case "FullyArchived": |
| 53 | + { |
| 54 | + 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."); |
| 55 | + if (Force || ShouldProcess(Identity.Url)) |
| 56 | + { |
| 57 | + spoOperation = Tenant.UnarchiveSiteByUrl(Identity.Url); |
| 58 | + AdminContext.Load(spoOperation); |
| 59 | + AdminContext.ExecuteQueryRetry(); |
| 60 | + WriteObject("Reactivation in progress. It may take upto 24hrs for reactivation to complete."); |
| 61 | + } |
| 62 | + break; |
| 63 | + } |
| 64 | + case "RecentlyArchived": |
| 65 | + { |
| 66 | + string resourceString = "Reactivating a site from \"Recently Archived\" state is free. Site will be available as an Active site soon."; |
| 67 | + WriteObject(resourceString); |
| 68 | + if (Force || ShouldProcess(Identity.Url)) |
| 69 | + { |
| 70 | + spoOperation = Tenant.UnarchiveSiteByUrl(Identity.Url); |
| 71 | + AdminContext.Load(spoOperation); |
| 72 | + AdminContext.ExecuteQueryRetry(); |
| 73 | + } |
| 74 | + break; |
| 75 | + } |
| 76 | + case "NotArchived": |
| 77 | + WriteObject("The site is already in Active state and cannot be reactivated."); |
| 78 | + return; |
| 79 | + case "Reactivating": |
| 80 | + WriteObject("The site is already reactivating and cannot be reactivated."); |
| 81 | + return; |
| 82 | + } |
| 83 | + break; |
| 84 | + } |
| 85 | + default: |
| 86 | + throw new InvalidOperationException("OperationAborted"); |
| 87 | + } |
| 88 | + if (!NoWait.ToBool()) |
| 89 | + { |
| 90 | + PollOperation(spoOperation); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments