Friday 5 April 2013

Space Utility Tool


Problem Statement :

In SharePoint 2010, if we want to view the usage of data i.e., to enumerate how many # of list items being present in each list of a web application/site collection, is a tedious task which includes to customize a view that too works for a single site only. And if we go for C# way, the time for execution the entire site collection is heavy on both the SP server and client object models. For this, a handy SP PowerShell Script is being written, which does the job in no matter of time.

clear;
function getLastPart($url)
{
 $tempUrl = $url.Replace('/', ';')
 $val = $tempUrl.split(";");
 return $val[$val.Length - 1];
}
$TotalLists = 0;
$TotalListItems = 0;
$TotalItems = 0
$mySiteUrl = Read-Host "Enter the Url :: ";
$lstPart = getLastPart($mySiteUrl);
$mySiteUrl|out-String;
$SiteCollection = Get-SPSite $mySiteUrl;
$fileName = DateTime.Now;
$sw = New-Object System.IO.StreamWriter("D:\\Neran\ListInfoLogs\" +$fileName+ ".txt");
ForEach ($Site in $SiteCollection.AllWebs)
{
    $sw.writeline("<------------------------------------------------------------------->");
    $sw.writeline("Site Url :: " +$Site.Url);
    $ListCount = 0;
    $TotalItems = 0;
    ForEach ($List in $Site.Lists)
    {  
     $sw.writeline($List.Title+ " :: "+ $List.ItemCount);
     $TotalItems += $List.ItemCount
      $ListCount += 1;
        }
    $TotalListItems += $TotalItems;
    $TotalLists += $ListCount;
    $sw.writeline("Total # of Lists in Site "+ $Site.Url +" :: " +$ListCount);
    $sw.writeline("Total # of List Items in Site " + $Site.Url +" :: " +$TotalItems);
    $sw.writeline("<------------------------------------------------------------------->");
}
Write-Host "Total number of Lists: " $TotalLists;
Write-Host "Total number of ListItems: " $TotalListItems;
$sw.writeline("<------------------------------------------------------------------->");
$sw.writeline("Total # of Lists in Site Collection "+ $mySiteUrl +" :: " +$TotalLists);
$sw.writeline("Total # of List Items in Site Collection " +$mySiteUrl +" :: " +$TotalListItems);
$sw.writeline("<------------------------------------------------------------------->");
$sw.Dispose();
$sw.Close();


How to Use/Execute the Code :
1) Download the script and copy it to any of your local folder (say D:\\PS Scripts\)
2) Open the SP 2010 Management Shell ( Start -> All Programs -> MS SP 2010 products -> SharePoint 2010 Management Shell)
3) Navigate to the folder D:\\PS Scripts\ that contains the script
4) Execute the script by typing “.\SpaceUtility.ps1″
5) Prompts for the SP Site Collection Url :: (Enter the Url)
6) The console will provide you the results and the same will be written in the file named
  D:\\PS Scripts\Last_Part_Of_Your_Url.txt
Uses :
This script will be useful for SP Admins during Migration of Data from one server to another and at times of upgrading the SP 2007 to SP 2010.

No comments:

Post a Comment