|
| 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 |
0 commit comments