Skip to content

Commit 3ded418

Browse files
authored
Merge pull request #3714 from gautamdsheth/feature/3700
Feature #3700 - added cmdlet to change site archive state.
2 parents a11e81a + 7aabec3 commit 3ded418

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPSiteArchiveState.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Set-PnPSiteArchiveState
8+
---
9+
10+
# Set-PnPSiteArchiveState
11+
12+
## SYNOPSIS
13+
14+
**Required Permissions**
15+
16+
* SharePoint: Access to the SharePoint Tenant Administration site
17+
18+
Sets the archived state of the site. Can be used to archive and reactivate sites.
19+
20+
## SYNTAX
21+
22+
```powershell
23+
Set-PnPSiteArchiveState -Identity <SPOSitePipeBind> -ArchiveState <SPOArchiveState> [-NoWait] [-Force]
24+
```
25+
26+
## DESCRIPTION
27+
28+
Use this cmdlet to change the archive status of the site. You must be a SharePoint Online administrator or Global administrator and be a site collection administrator to run the cmdlet.
29+
Microsoft 365 Archive needs to be enabled for the organization to be able to use this feature.
30+
31+
## EXAMPLES
32+
33+
### Example 1
34+
35+
```powershell
36+
Set-PnPSiteArchiveState https://contoso.sharepoint.com/sites/Marketing -ArchiveState Archived
37+
```
38+
39+
This example marks the site as Archived. For seven days after the operation, the site will remain in a "RecentlyArchived" state, where any reactivations will be free and instantaneous. If a site is reactivated after seven days, any reactivations will be charged and will take time.
40+
41+
### Example 2
42+
43+
```powershell
44+
Set-PnPSiteArchiveState https://contoso.sharepoint.com/sites/Marketing -ArchiveState Active
45+
```
46+
47+
This example triggers the reactivation of a site. If the site is reactivated from the "RecentlyArchived" state, it will become available instantaneously. If the site is reactivated from the "FullyArchived" state, it may take time for it to be reactivated.
48+
49+
## PARAMETERS
50+
51+
### -Identity
52+
Specifies the full URL of the SharePoint Online site collection that needs to be renamed.
53+
54+
```yaml
55+
Type: SPOSitePipeBind
56+
Parameter Sets: (All)
57+
58+
Required: True
59+
Position: Named
60+
Default value: None
61+
Accept pipeline input: True
62+
Accept wildcard characters: False
63+
```
64+
65+
### -ArchiveState
66+
67+
Sets the archived state of the site. Valid values are Archived, Active.
68+
69+
```yaml
70+
Type: SPOArchiveState
71+
Parameter Sets: (All)
72+
73+
Required: True
74+
Position: Named
75+
Default value: None
76+
Accept pipeline input: False
77+
Accept wildcard characters: False
78+
```
79+
80+
### -NoWait
81+
If specified the task will return immediately after creating the archive state site job.
82+
83+
```yaml
84+
Type: SwitchParameter
85+
Parameter Sets: (All)
86+
87+
Required: False
88+
Position: Named
89+
Default value: None
90+
Accept pipeline input: False
91+
Accept wildcard characters: False
92+
```
93+
94+
### -Force
95+
If provided, no confirmation will be asked for changing the archive state.
96+
97+
```yaml
98+
Type: SwitchParameter
99+
Parameter Sets: (All)
100+
101+
Required: False
102+
Position: Named
103+
Default value: None
104+
Accept pipeline input: False
105+
Accept wildcard characters: False
106+
```
107+
108+
## RELATED LINKS
109+
110+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PnP.PowerShell.Commands.Enums
2+
{
3+
public enum SPOArchiveState
4+
{
5+
Archived,
6+
Active
7+
}
8+
}

0 commit comments

Comments
 (0)