Friday, January 15, 2016

Set all Site Collections to Read-Only via Powershell

This is a great script that helps with a migration.  We've found that setting sites to Read Only as you migrate from one environment to another allows users to see the old farm while being forced to use the new farm. 

Add-PSSnapin Microsoft.SharePoint.Powershell

# Define whether ReadOnly will be set to true or false
[bool]$readOnly = $true

# Set excluded paths as comma-delimited strings.  Any sites listed below will NOT be changed. 
# Note: Web applications must end in a trailing slash, where site collections do not
[array]$excludedPaths = "http://apps.insidegulfstream.com/",
                        "http://some.sharepointsite.com/",
                        "http://another.sharepointsite.com/somesitecollection",
                        “http://athird.sharepointsite.com/”

# Get all Web applications (except Central Admin)
Get-SPWebApplication | ForEach-Object {
 
    if ($excludedPaths -notcontains $_.Url)
    {
        # Enumerate all content databases in each Web application
        if ($_.ContentDatabases -ne $null) {
            $_.ContentDatabases | ForEach-Object {
         
                # Enumerate all site collections in each content database
                if ($_.Sites -ne $null) {
                    $_.Sites | ForEach-Object {
                     
                        # Check if there are sites where the property should not be changed
                        if ($excludedPaths -notcontains $_.Url)
                        {
                            Write-Host "Changing ReadOnly property for site" $_.Url
                         
                            # Set ReadOnly property
                            $_.ReadOnly = $readOnly
                            Write-Host "ReadOnly property for site" $_.Url "set to" $_.ReadOnly
                         
                            # Dispose site collection object
                            $_.Dispose()
                        }
                        else
                        {
                            # Confirm if no changes made on excluded sites
                            Write-Host "No changes made for site" $_.Url
                        }
                    }
                }
            }
        }
    }
    else
    {
        # Confirm if no changes made on excluded web applications
        Write-Host "No changes made for web application" $_.Url
    }
}

# Get all Web applications (except Central Admin)
Get-SPWebApplication | ForEach-Object {
    # Enumerate all content databases in each Web application
    if ($_.ContentDatabases -ne $null) {
        $_.ContentDatabases | ForEach-Object {
            # Enumerate all site collections in each content database
            if ($_.Sites -ne $null) {
                $_.Sites | ForEach-Object {
                    Write-Host "ReadOnly property for site" $_.Url "set to" $_.ReadOnly
                    # Dispose site collection object
                    $_.Dispose()
                }
            }
        }
    }
}

No comments:

Post a Comment