Script which automatically unlock site collection - Sharepoint powershell

Posted by Leszek Polnik | Posted in | Posted on 04:36

0

If you need to check or unlock site collection(s) automatically you can use this script. You can also add it at the end of any stsadm backup script by adding:

  powershell.exe "& 'c:\folder\Get-SiteLock.ps1' -unlock"
 
This way it will check all site collections of all web applications for read-only locks and unlocks them if found. Post SP2 site collection backup sets the site collection as read-only during the backup but sometimes doesn't unlock them correctly after the backup.


 
Get-SiteLock.ps1:

 
param ([string[]]$url, [switch]$help, [switch]$unlock)
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0 , Culture=Neutral, PublicKeyToken=71e9bce111e9429c") | Out-Null
$ErrorActionPreference = "Stop";
$hive = "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12"

 
function GetHelp() {
    $HelpText = @"

 
    DESCRIPTION:
    NAME: Get-SiteLock.ps1
    Gets Information about SiteLock of Site Collections

 
    PARAMETERS:
    -url WebApplication URL (optional). If not specified checks all SCs of all webapps
    -unlock (optional). If not specified current lock status will be displayed
    -help Prints this help (optional)

 
    EXAMPLES:
    Get-SiteInformation.ps1 -url http://wss
        checks alls SC of WebApss at specified URL

 
    Get-SiteInformation.ps1 -url http://wss -unlock
        unlocks alls SC of WebApss at specified URL

 
    Get-SiteInformation.ps1
        checks all SCs of all WebApps

 
    Get-SiteInformation.ps1 -unlock
        unlocks all SCs of all WebApps
   
    Created by Martin.Petvalsky@tieto.com
"@
    $HelpText
}

 
function SiteInformation ([string]$url2, [switch]$unlock2) {
    if ($url2)
    {
        $webapps = $url2 | ForEach-Object {[Microsoft.SharePoint.Administration.SPWebApplication]::Lookup([uri]$_)};
    }
    else
    {
        $webapps = ([Microsoft.SharePoint.Administration.SPFarm]::Local.Services |
            Where-Object {$_.TypeName -eq "Windows SharePoint Services Web Application"}).WebApplications;
    }

    $webapps | Foreach-Object `
    {
        if (!$unlock)
        {
            write-host "`nChecking site collection(s) of the `""$_.name"`" webapplication for read-only locks:"
            foreach ($site in $_.Sites)
            {
                write-host "`tSite collection URL: `t" $site.URL
                if ($site.ReadOnly)
                {
                    write-host "`tis in read-only mode!" -foregroundcolor red
                }
                $site.dispose()
            }
        }
        else
        {
            write-host "`nUnlocking read-only site collection(s) of the `""$_.name"`" webapplication:"
            foreach ($site in $_.Sites)
            {
                if ($site.ReadOnly)
                {
                    write-host "`tSite collection URL: `t" $site.URL
                    write-host "`tis in read-only mode, unlocking" -foregroundcolor red
                    $site.ReadOnly = $false
                }
                $site.dispose()
            }
        }
    }
}

 
if ($help)
{
    GetHelp
}
else
{
    if (!$url -AND !$unlock) { SiteInformation $null }
    if (!$url -AND $unlock) { SiteInformation $null -unlock }
    if ($url -AND !$unlock) { SiteInformation $url }
    if ($url -AND $unlock) { SiteInformation $url -unlock }
    write-host $locked_count $unlocked_count
}

Comments Posted (0)

Post a Comment