Follow Us:
Awhile back I ran into an issue where I had some site collection backups that failed to complete. No big deal, but this caused the locks on the site collection to remain in place, as I curiously found my administrator account with deny permissions on all sites in the site collection. This begs the question “What other site collections could be locked?” That becomes a real problem if you have a large number of site collections. Who wants to check each one in Central Administration one by one? Being the non-developer type, I turn to my trusty friend PowerShell.
First, what are we talking about specifically? In Central Administration, click Application Management, then under Site Collections, click Configure quotas and locks. Choose your web application/site collection, and view it’s status:
Before we look at unlocking it to solve our initial problem, let’s take a quick peek at how to set a lock as things are a little different. We have a few options.
Where Lock-Type is: Unlock NoAdditions ReadOnly NoAccess
Where Lock-Type is:
A site lock is also set automatically when a site collection backup is run (add –nositelock on the command to prevent this). As a side note, the fine folks at Microsoft made a goof in the help text for the Set-SPSite cmdlet (or updated the command without fixing the help text). For the Adding Content Prevented selection, the help for the command says to use Content for the lock type. This is wrong and the command will error. That’s really helpful! You should use NoAdditions.
So I needed a way to show me the lock status of all site collections in the farm. I searched around and couldn’t find one so I decided to write one. If you just need to get the status of one site collection, that’s easy. You could use STSADM:
stsadm -o getsitelock -url <Site-collection-url>
Unfortunately, there really isn’t a one-line equivalent in PowerShell to “get” locks for site collections that I know of. If you list out the properties of the site collection object, there is no property called “Lock State” or similar. The lock values shown in the UI are actually stored across 4 different properties in the site collection object:
Let’s see how this works. Go into the UI, set a value (or use the PowerShell or STSADM above). Then you can see the combination of values of the settings by running the following PowerShell command:
Get-SPSite <Site-collection-url> | select ReadOnly,Readlocked,WriteLocked,LockIssue | ft –autosize
So now you work out the combinations of values for each “lock type”. Here they are:
When No Access is set, that puts an explicit Deny permission on the site collection, which prevents the values from being read. Therefore it shows null values. Who wants to remember all that? How can we tie all this together and get a value for these easily?
Now comes the fun part. We know the value combinations, but how can we see the single UI value for all site collections? Use this script.
1: Add-pssnapin Microsoft.SharePoint.Powershell -ErrorAction silentlycontinue
2: $sites = get-spsite -limit all | foreach {
3: write-host "Checking lock for site collection: " $_.RootWeb.Title -foregroundcolor blue
4: if ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $false)
5: { write-host "The site lock value for the site collection"$_.RootWeb.Title "is: Unlocked" -foregroundcolor Green}
6: if ($_.lockissue -ne $null) {
7: write-host "The additional text was provided for the lock: " $_.LockIssue -foregroundcolor Green}
8: elseif ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true)
9: { write-host "The site lock value for the site collection"$_.RootWeb.Title "is: Adding Content Prevented" -foregroundcolor Green}
10: elseif ($_.ReadOnly -eq $true -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true)
11: { write-host "The site lock value for the site collection"$_.RootWeb.Title "is: Read-only" -foregroundcolor Green}
12: elseif ($_.ReadOnly -eq $null -and $_.ReadLocked -eq $null -and $_.WriteLocked -eq $null)
13: { write-host "The site lock value for the site collection"$_.RootWeb.Title "is: No Access" -foregroundcolor Green}
14: }
What are we doing here?
The output will look like this:
Sweet! I will try and work on making this into a nice table for export, but for now it gets the job done. You could certainly extend the script with additional nesting that when it found something other than Unlocked, it would run the Set-SPSite command to undo the lock. Enjoy!
AlyssonDK, There are in-depth scripts like Phil's at http://get-spscripts.com/2012/07/setting-multiple-site-collections-to.html, but I found a simple way in 1 line to set all your site collections to readonly via the site lock. get-spsite -limit all | set-spsite -Lockstate "ReadOnly" This will get all site collections in all web applications and set their site collection lock to ReadOnly. Get the values at the bottom of the page here: http://technet.microsoft.com/en-us/library/ff631148(v=office.14).aspx If you want to limit your scope a little bit and only set site collections that start with a url, you can do something like this: get-spsite -limit all | Where-Object {$_.Url -like "https://sp13lab*"} | set-spsite -Lockstate "ReadOnly" To unlock, just change ReadOnly to Unlock. I tested this in a SP13 environment and it worked just fine. Hope it helps! Doug
How will you set a boolean field value to null in the following of your code. When UI value “No Access” ReadOnly = null ReadLocked = null WriteLocked = null The fields above shown are all boolean , they will not accept null as a valid value. Then what approach should be taken to make a site no access using server side or client side code.
@Sajjad, You don't need to set each value to null in PowerShell. At the very stop, you use a one line stsadm or Powershell command to set the lock type like NoAccess. That will set all the values appropriately. But typically with PowerShell, to set Boolean it's typically $var = $true or $var = $false to show whether its enabled or disabled. Booleans really shouldn't be null in this case. If other cases, they might be if they've never been set to a value, then to set that it's typically $var = $null. Doug
I rewrote some of it to output an array of objects results for each site. Now it can be formatted however you want. Thanks for the start this work's very well now.Add-pssnapin Microsoft.SharePoint.Powershell -ErrorAction silentlycontinue$siteResult = @()$sites = get-spsite -limit all | foreach { $objResult = New-Object System.Object $objResult | Add-Member -type NoteProperty -name SiteName -value $_.RootWeb.Title if ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $false) { $objResult | Add-Member -type NoteProperty -Name Status -value "Unlocked" } if ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true) { write-host "trap1" $objResult | Add-Member -type NoteProperty -Name Status -value "Adding Content Prevented" } if ($_.ReadOnly -eq $true -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true) { $objResult | Add-Member -type NoteProperty -Name Status -value "Read-Only" } if ($_.ReadOnly -eq $null -and $_.ReadLocked -eq $null -and $_.WriteLocked -eq $null) { $objResult | Add-Member -type NoteProperty -Name Status -value "No Access" } if ($_.lockissue -ne $null) { $objResult | Add-Member -type NoteProperty -Name LockInfo -value $_.LockIssue } $siteResult += $objresult }$siteResult
Thanks a lot, you saved my time
Hi I have a very similar problem. However, I need the script to change all to read only. Can you help me?
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.