Skip to content

Commit 0019c56

Browse files
Merge pull request #159 from PowershellScripts/structure-fixes
structure fix
2 parents 66668e2 + b7ab708 commit 0019c56

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
29.5 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
function Set-VersionLimit
2+
{
3+
param (
4+
[Parameter(Mandatory=$true,Position=1)]
5+
[string]$Username,
6+
[Parameter(Mandatory=$true,Position=2)]
7+
[string]$Url,
8+
[Parameter(Mandatory=$true,Position=3)]
9+
$Password,
10+
[Parameter(Mandatory=$true,Position=4)]
11+
[int]$VersionLimit,
12+
[Parameter(Mandatory=$false,Position=5)]
13+
[bool]$Versioning
14+
)
15+
16+
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
17+
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
18+
$ctx.Load($ctx.Web.Lists)
19+
$ctx.Load($ctx.Web)
20+
$ctx.Load($ctx.Web.Webs)
21+
$ctx.ExecuteQuery()
22+
23+
Write-Host $ctx.Url -BackgroundColor White -ForegroundColor DarkGreen
24+
25+
foreach($list in $ctx.Web.Lists)
26+
{
27+
$csvvalue= New-Object PSObject
28+
$csvvalue | Add-Member -MemberType NoteProperty -Name "Previous Versioning Status" -Value $list.EnableVersioning
29+
30+
# $list.EnableVersioning = $Versioning
31+
$list.MajorVersionLimit = $VersionLimit
32+
$list.Update()
33+
$listurl=$null
34+
35+
if($ctx.Url.EndsWith("/"))
36+
{
37+
$listurl= $ctx.Url+$list.Title
38+
}
39+
else
40+
{
41+
$listurl=$ctx.Url+"/"+$list.Title
42+
}
43+
44+
45+
$csvvalue | Add-Member -MemberType NoteProperty -Name "Url" -Value ($listurl)
46+
$csvvalue | Add-Member -MemberType NoteProperty -Name "Title" -Value $list.Title
47+
$csvvalue | Add-Member -MemberType NoteProperty -Name "Status" -Value "Failed"
48+
49+
try
50+
{
51+
$ErrorActionPreference="Stop"
52+
$ctx.ExecuteQuery()
53+
Write-Host $listurl -ForegroundColor DarkGreen
54+
$csvvalue.Status="Success"
55+
$Global:csv+= $csvvalue
56+
}
57+
catch
58+
{
59+
$Global:csv+= $csvvalue
60+
Write-Host $listurl $_.Exception.Message -ForegroundColor Red
61+
}
62+
finally
63+
{
64+
$ErrorActionPreference="Continue"
65+
}
66+
67+
68+
}
69+
70+
if($ctx.Web.Webs.Count -gt 0)
71+
{
72+
for($i=0; $i -lt $ctx.Web.Webs.Count ; $i++)
73+
{
74+
Set-VersionLimit -Username $Username -Url $ctx.Web.Webs[$i].Url -Password $Password -VersionLimit $VersionLimit -Versioning $Versioning
75+
}
76+
}
77+
78+
79+
80+
}
81+
82+
# Paths to SDK. Please verify location on your computer.
83+
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
84+
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
85+
86+
87+
88+
# Versioning will be enabled. If you prefer to disable it for the whole tenant, change to $false
89+
$Versioning = $true
90+
91+
#Number of major versions to keep
92+
$VersionLimit = 14
93+
94+
# You can also enter credentials directly: $siteUrl="https://tenant-admin.sharepoint.com"
95+
$AdminUrl = "https://tenant-admin.sharepoint.com
96+
$Username = "test@tenant.onmicrosoft.com"
97+
$Password = Read-Host -Prompt "Enter password" -AsSecureString
98+
$Creds= New-Object System.Management.Automation.PSCredential($username,$password)
99+
Connect-SPOService -Credential $Creds -Url $AdminUrl
100+
101+
$sitecollections=Get-SPOSite
102+
$Global:csv=@()
103+
104+
#Uncomment the foreach loop if you want to change the settings in all site collections
105+
#foreach($sitecoll in $sitecollections)
106+
#{
107+
Set-VersionLimit -Url ("https://test.sharepoint.com/sites/test") -Username $Username -Password $Password -Versioning $Versioning -VersionLimit $VersionLimit
108+
#}
109+
110+
111+
112+
# Specify the path where the log file will be published
113+
$Global:csv | Export-Csv -Path C:\Users\Public\Versioninglimitversion.csv
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
A short script that sets the major version limit for all the libraries and lists in the whole site collection or in the whole tenant.
2+
3+
It requires SharePoint Online Management Shell and SharePoint SDK installed:
4+
5+
http://technet.microsoft.com/en-us/library/fp161372(v=office.15).aspx
6+
7+
http://www.microsoft.com/en-us/download/details.aspx?id=30722
8+
9+
10+
11+
It uses recurrence to find all sites in all site collections and then goes through all the lists.
12+
13+
If the list doesn't have versioning enabled, modifying the major version limit is not possible and you will receive the following error message:
14+
15+
Exception calling "ExecuteQuery" with "0" argument(s): "Specified method is not supported."For some lists, enabling version may not be possible and you will receive a notification of that.
16+
17+
At the end, a csv file is generated with the lists' urls and the status whether the setting was successful or not.
18+
19+
20+
21+
As the script runs you will see green lists' titles for which the setting succeeded and red for those which failed:
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
In order to use the script, adjust the data inside:
36+
37+
PowerShell
38+
# Paths to SDK. Please verify location on your computer.
39+
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
40+
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
41+
42+
43+
44+
# Versioning will be enabled. If you prefer to disable it for the whole tenant, change to $false
45+
$Versioning = $true
46+
47+
#Number of major versions to keep
48+
$VersionLimit = 14
49+
50+
# You can also enter credentials directly: $siteUrl="https://tenant-admin.sharepoint.com"
51+
$AdminUrl = "https://tenant-admin.sharepoint.com”
52+
$Username = "test@tenant.onmicrosoft.com"
53+
$Password = Read-Host -Prompt "Enter password" -AsSecureString
54+
$Creds= New-Object System.Management.Automation.PSCredential($username,$password)
55+
Connect-SPOService -Credential $Creds -Url $AdminUrl
56+
57+
$sitecollections=Get-SPOSite
58+
$Global:csv=@()
59+
60+
#Uncomment the foreach loop if you want to change the settings in all site collections
61+
#foreach($sitecoll in $sitecollections)
62+
#{
63+
Set-VersionLimit -Url ("https://test.sharepoint.com/sites/test") -Username $Username -Password $Password -Versioning $Versioning -VersionLimit $VersionLimit
64+
#}
65+
66+
67+
68+
# Specify the path where the log file will be published
69+
$Global:csv | Export-Csv -Path C:\Users\Public\Versioninglimitversion.csv
70+
59 KB
Loading

0 commit comments

Comments
 (0)