Update: Use PowerShell to make an entire web application read-only


For those who have been following my posts for a while, they’re probably wondering why I’m blogging about something I’ve already done.

The answer is simple: I found a better way!

In the 7 months since I wrote a post on how to make an entire web application read-only, I’ve learned quite a bit about PowerShell, and the ForEach-Object cmdlet. I now have a better version of the advanced function I showed off back in March, and this one has a new switch parameter (-AllContentWebApplications) which obviously allows you to run the Set-SPSiteLockState function against all web applications (excluding Central Admin).

I like this version a LOT better, and it’s much cleaner. If you only need to run the function against a single web app, that functionality still exists – just exclude the switch param.

Here it is!

function Set-SPSiteLockState {
<#
.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>Set-SPSiteLockState -WebAppUrl http://intranet -LockState ReadOnly
	This example sets all site collections in a web application at http://intranet to read-only.
.Example
	C:\PS>Set-SPSiteLockState -AllContentWebApplications -LockState ReadOnly
	This example sets all web applications to read-only.
.Notes
	Name: Set-SPSiteLockState
	Author: Ryan Dennis
	Last Edit: 10/14/2011
	Keywords: Set Lock State, Set-SPSiteAdministration, Set-SPSiteLockState
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
.Inputs
	None
.Outputs
	None
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[string]$WebAppUrl,
[string]$LockState=(Read-Host "Please enter a Lock State (Examples: Unlock, NoAccess, ReadOnly)"),
[switch]$AllContentWebApplications
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Start-SPAssignment -Global
if($AllContentWebApplications){
Write-Host "Setting all web applications to $($LockState)..."
$WebApp = Get-SPWebApplication
	$WebApp | ForEach-Object{
	$AllSites = $WebApp | Get-SPSite -Limit All -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
		$AllSites | ForEach-Object{
		Set-SPSiteAdministration -LockState $LockState -Identity $_.url
		}
	}
}
else{
$WebApp = Get-SPWebApplication $WebAppUrl
$AllSites = $WebApp | Get-SPSite -Limit All -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Write-Host "Setting $WebAppUrl to $lockState..." -ForegroundColor Yellow
$AllSites | ForEach-Object { Set-SPSiteAdministration -LockState $lockState -Identity $_.url }
}

Stop-SPAssignment -Global
Write-Host "Finished!" -ForegroundColor Green
}
Advertisement

One thought on “Update: Use 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