Question

I am trying to build a Powershell Script, which iterates over all SiteCollections and checks if they are on lockdown.

This is because, sometimes some collections don't come back from read-only, after the nightly backup. If a SC is on read-only, I want to set it to unlock.

This is my code so far:

$sites = get-spsite -limit all | foreach 
{
    write-host "Site Collection: " $_.RootWeb.Title
    if (  $_.ReadOnly -eq $true)
    { 
         write-host "Site Collection: "$_.RootWeb.Title "--- Read-only" 
         //How to unlock?
         //Set-SPSite -identity $_.RootWeb -lockstate unlock
         //$_.ReadOnly = $false
    }
}

I can't verify if my unlock code is working, because I have no testing environment at hand right now. I need help for the commented line on how to correctly unlock $_.RootWeb

Kind regards

/edit: To make things clear. The problem is not, that I do not have test environments. The problem is, I can't access them right now. I can't figure out from the top of my head how i can set the readonly property to false or how to get the -identity from $_. to get the Set-SPSite working in the foreach loop.

Was it helpful?

Solution

The following POC code works for me:

foreach($site in Get-spsite "PORTALURL/*" -limit all)
{
    Write-Host $site.RootWeb.Url
    $site.ReadOnly = $true
    Write-Host "Is read only:" $site.ReadOnly
    $site.ReadOnly = $false
    Write-Host "Is read only:" $site.ReadOnly
    $site.Dispose()
}

It first successfully sets a site as Read Only, and thereafter unsets it.

OTHER TIPS

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:

  • ReadOnly
  • ReadLocked
  • WriteLocked
  • LockIssue

To get the list of locked site collections, you can make use of the below powershell script.

Add-pssnapin Microsoft.SharePoint.Powershell -ErrorAction silentlycontinue
$sites = get-spsite -limit all | foreach
{
    write-host "Checking lock for site collection: " $_.RootWeb.Title -foregroundcolor blue
    if ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $false)
    {
        write-host "The site lock value for the site collection"$_.RootWeb.Title "is:  Unlocked" -foregroundcolor Green
    }
    if ($_.lockissue -ne $null)
    {
        write-host "The additional text was provided for the lock: " $_.LockIssue -foregroundcolor Green
    }
    elseif ($_.ReadOnly -eq $false -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true)
    {
        write-host "The site lock value for the site collection"$_.RootWeb.Title "is:  Adding Content Prevented" -foregroundcolor Green
    }
    elseif ($_.ReadOnly -eq $true -and $_.ReadLocked -eq $false -and $_.WriteLocked -eq $true)
    {
        write-host "The site lock value for the site collection"$_.RootWeb.Title "is:  Read-only" -foregroundcolor Green
    }
    elseif ($_.ReadOnly -eq $null -and $_.ReadLocked -eq $null -and $_.WriteLocked -eq $null)
    {
        write-host "The site lock value for the site collection"$_.RootWeb.Title "is:  No Access" -foregroundcolor Green
    }
}

After running the above script you will get to know which all site collections are locked, using the below code you can unlock it, instead of setting unlock for all the site collections,

using STSADM stsadm -o setsitelock -url <Site-collection-url> -lock Unlock

Using PowerShell Set-SPSite -identity "<Site-collection-url>" –lockstate Unlock

Source

According to this TechNet article for SharePoint 2010, the PowerShell code to lock or unlock a Site Collection is as follows:

Set-SPSite -Identity "<SiteCollection>" -LockState "<State>"

Where SiteCollection is the URL of the site collection that you want to lock or unlock.

And State is one of the following values:

  • Unlock to unlock the site collection and make it available to users.
  • NoAdditions to prevent users from adding new content to the site collection. Updates and deletions are still allowed.
  • ReadOnly to prevent users from adding, updating, or deleting content.
  • NoAccess to prevent users from accessing the site collection and its content. Users who attempt to access the site receive an error

So to unlock, the LockState should be set to Unlock

But I really advise you to set up a test-environment to try this out. Set up a SharePoint Foundation environment in a Virtual Machine.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top