Monday, February 4, 2013

How to find the sites been discarded by users?

After a few years of operation, normally there are quite a lot of site collections in SharePoint farm. Many site collections are created for projects or company activities, and the site owners may forget to notifiy site administrator that the site collections are not needed any more.

As a SharePoint administrator, I need to find out out those sites which should be removed/archived from farm. These sites should be moved to "Archiving" content database, they should be in "read only" mode, or it should not be accessable to normal users.

This can improve the SharePoint farm performance, and search engine doesn't need to crawl the data stored there everyday.

That's why I wrote the PowerShell script below. The field "Last Modified" and "Last Modified By" are displayed in the search result along with web url and list url.  The only input parameter is the Web Application Url.

I hope this can save you some time.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

cls

$TargetDate = (Get-Date).AddDays(-30)
Write-Host "TargetDate: " $TargetDate

function GetLastModifiedUser([Microsoft.SharePoint.SPList]$spListCurrent)
{
$spQuery = New-Object Microsoft.SharePoint.SPQuery;
$camlQuery = "";
$spQuery.Query = $camlQuery;
$spQuery.RowLimit = 1;
$spListItemCollection = $spListCurrent.GetItems($spQuery)
$spListItem = $spListItemCollection[0];
$UserLookup = New-Object Microsoft.SharePoint.SPFieldLookupValue($spListItem["Editor"]);
return $UserLookup.LookupValue;
}

function ListLastModifiedLists([string]$webApplicationURL)
{
Write-Host "webApplicationURL:" $webApplicationURL
$webApp = Get-SPWebApplication $webApplicationURL
foreach($site in $webApp.Sites)
{
try {
if ($site.ReadLocked -ne $false)
{
Write-Host "Site:" $site.ServerRelativeUrl "`tReadLocked: true"
continue
}
} catch [Exception] {
{continue}
}
Write-Host "Site:" $site.ServerRelativeUrl "`tWriteLocked:" $site.WriteLocked

foreach($web in $site.AllWebs)
{
Write-Host " Web:" $web.ServerRelativeUrl

$ListItemCount = 0
$LastModifiedList = ""
$LastModifiedBy = ""
$LastModified = (Get-Date).AddYears(-10)

foreach($list in $web.Lists)
{
#special lists updated by scheduled workflow automatically, which should be skipped

if(($list.Title -eq "EFSimpleReminderHistory") -or ($list.Title -eq "User Information List") -or ($list.Title -eq "Workflow History"))
{continue}
elseif($list.ItemCount -eq 0)
{continue}
elseif($list.LastItemModifiedDate -gt $LastModified)
{
$LastModified = $list.LastItemModifiedDate
$LastModifiedList = $list.Title
$ListItemCount = $list.ItemCount
$LastModifiedBy = GetLastModifiedUser $list
}
elseif($list.LastItemDeletedDate -gt $LastModified)
{
$LastModified = $list.LastItemDeletedDate
$LastModifiedList = $list.Title
$ListItemCount = $list.ItemCount
$LastModifiedBy = "Deleted"
}
}

if($LastModified -lt $TargetDate)
{
Write-Host " " $LastModifiedList "`t" $LastModified "`tBy" $LastModifiedBy "`tItemCount" $ListItemCount -ForegroundColor Red
}

$web.Dispose()
}
$site.Dispose()
}
}

ListLastModifiedLists "http://spserver"

Write-Host "Finished! Press enter key to exit." -ForegroundColor Green
Read-Host 

No comments:

Post a Comment