Using PowerShell to make an entire web application read-only


STSADM.exe was great in SharePoint 2007. It provided some really good functionality, some of which was not available in the GUI. STSADM is still available in SharePoint 2010, but why anybody would use it is beyond me. PowerShell is not only much more powerful (hence the name), but it’s here to stay.
There are loads of examples of scripts online for various SharePoint tasks, and there are lots of one-offs that I’ve made scripts for that I doubt anybody else would use; however, there is one thing I think everyone may want at some point, the ability to make an entire web application read-only. Could you make the content database read-only? Sure, but maybe you want just the sites to be read-only.
In 2007, you could set a site collection as read-only via STSADM; but I was never able to find a “good” way to make an entire web application read-only. In SharePoint 2010, it’s very easy with a few commands and a ForEach loop.
Here’s the advanced function:
function Set-LockState {
<#
.SYNOPSIS
Use this PowerShell script to set the Lock State of a SharePoint Web Application to Unlock, ReadOnly, NoAdditions or NoAccess.
.DESCRIPTION
This PowerShell script uses Set-SPSiteAdministration to set the Lock State of a SharePoint Web Application.
.EXAMPLE
C:\PS>.\SetAllSitesInAWebAppToReadOnly.ps1 -WebAppUrl http://intranet -LockState ReadOnly
This example sets all site collections in a web application at http://intranet to read-only.
.NOTES
This PowerShell script sets the Lock State of a SharePoint Web Application.
.LINK
http://www.iccblogs.com/blogs/rdennis
 http://twitter.com/SharePointRyan
.INPUTS
None
.OUTPUTS
None
#>

## Input Parameters ##
[CmdletBinding()]
Param(
[string]$WebAppUrl=(Read-Host "Please enter a Web Application URL (Example: http://intranet)"),
[string]$LockState=(Read-Host "Please enter a Lock State (Examples: Unlock, ReadOnly)")
)
## Add SharePoint Snap-in ##
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
### No need to modify these ###
$WebApp = Get-SPWebApplication $WebAppUrl
$AllSites = $WebApp | Get-SPSite

############################# The Script - no need to modify this section #############################
## Start SP Assignment for proper disposal of variables and objects ##
Write-Host "Starting SP Assignment..." -ForegroundColor Green
Start-SPAssignment -Global
## Use a ForEach-Object loop and Set-SPSiteAdministration to set an entire web application ##
Write-Host "Setting $WebAppUrl to $lockState..." -ForegroundColor Yellow
$AllSites | ForEach-Object { Set-SPSiteAdministration -LockState $lockState -Identity $_.url }
## End SP Assignment for proper disposal of variables and objects ##
Write-Host "Disposing of SP Objects.." -ForegroundColor Green
Stop-SPAssignment -Global
## Tell us it's done ##
Write-Host "Finished!" -ForegroundColor Green
}
Advertisement

2 thoughts on “Using PowerShell to make an entire web application read-only

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s