-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSet-DocumentLibraryVersioningSettings.ps1
126 lines (101 loc) · 4.4 KB
/
Set-DocumentLibraryVersioningSettings.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
Microsoft provides programming examples for illustration only, without warranty either expressed or
implied, including, but not limited to, the implied warranties of merchantability and/or fitness
for a particular purpose. We grant You a nonexclusive, royalty-free right to use and modify the
Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that
You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the
Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which
the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers
from and against any claims or lawsuits, including attorneys' fees, that arise or result from the
use or distribution of the Sample Code.
#>
<#
.SYNOPSIS
Apply versioning settings to all document libraries in a target site collection, optionally including subsites.
.DESCRIPTION
Apply versioning settings to all document libraries in a target site collection, optionally including subsites.
Excluded Libraries: Form Templates, Site Assets, Style Library
NOTE: This script requires the PowerShell module 'SharePointPnPPowerShellOnline' to be installed. If it is missing, the script will attempt to install it.
.PARAMETER SiteUrl
SharePoint Site Collection URL
.PARAMETER EnableMajorVersions
Enable major versioning on the document library
.PARAMETER EnableMinorVersions
Enable minor versioning on the document library
.PARAMETER MajorVersionsLimit
Major versions limit
.PARAMETER MinorVersionsLimit
Minor versions limit
.PARAMETER IncludeSubsites
Apply settings to document libraries on subsites if any exist
#>
param(
[parameter(Mandatory=$true)]$SiteUrl,
[bool]$EnableMajorVersions = $true,
[bool]$EnableMinorVersions = $true,
[int]$MajorVersionsLimit = 30,
[int]$MinorVersionsLimit = 10,
[switch]$IncludeSubsites
)
#############################################
$module = Get-Module -Name SharePointPnPPowerShellOnline -ListAvailable
if ($null -eq $module) {
Write-Output "Installing PowerShell Module: SharePointPnPPowerShellOnline"
Install-Module -Name SharePointPnPPowerShellOnline -Force -AllowClobber -Confirm:$false
}
Import-Module -Name SharePointPnPPowerShellOnline -WarningAction Ignore
$module = Get-Module -Name SharePointPnPPowerShellOnline
#############################################
$ExcludedLibraryTitles = @("Form Templates", "Site Assets", "Style Library")
#############################################
Function Process-Web
{
param([parameter(Mandatory=$true)]$Web)
begin
{
#Library Settings
$LibrarySettings = @{
EnableVersioning = $EnableMajorVersions
EnableMinorVersions = $EnableMinorVersions
MajorVersions = $MajorVersionsLimit
MinorVersions = $MinorVersionsLimit
}
}
process
{
Write-Host "[Site] $($Web.Url)" -ForegroundColor Cyan
#Document Libraries that are not Excluded
$ListIncludes = @("Title", "EnableVersioning", "EnableMinorVersions", "MajorVersionLimit", "MajorWithMinorVersionsLimit")
$Libraries = Get-PnPList -Web $Web -Includes $ListIncludes | Where-Object { $_.BaseTemplate -eq 101 -and $ExcludedLibraryTitles -notcontains $_.Title }
#Update Each Library
foreach ($Library in $Libraries)
{
if ($Library.EnableVersioning -ne $EnableMajorVersions -or $Library.EnableMinorVersions -ne $EnableMinorVersions `
-or $Library.MajorVersionLimit -ne $MajorVersionsLimit -or $Library.MajorWithMinorVersionsLimit -ne $MinorVersionsLimit)
{
Write-Host " [Updating Library] $($Library.Title)" -ForegroundColor Green
Set-PnPList -Identity $Library -Web $Web @LibrarySettings
}
else
{
Write-Host " [Skipped Library - No Changes] $($Library.Title)"
}
}
}
end
{
}
}
#############################################
Write-Host "Connecting to SharePoint Site Collection URL: $SiteUrl"
Connect-PnPOnline -Url $SiteUrl
$RootWeb = Get-PnPWeb
Process-Web -Web $RootWeb
#Recursively Process Subsites if -IncludesSubsites was specified
if ($IncludeSubsites)
{
foreach ($SubWeb in (Get-PnPSubWebs -Web $RootWeb -Recurse))
{
Process-Web -Web $SubWeb
}
}