Wednesday, April 23, 2014

Output Site Collection Information Using PowerShell

Windows PowerShell is a fantastic tool; SharePoint 2010 has literally hundreds of different PowerShell Cmdlets that are available out of the box,

if you don’t believe me check this out - http://technet.microsoft.com/en-us/library/ff678226.aspx. What about MOSS 2007? Whilst there aren’t any native Cmdlets for MOSS 2007, PowerShell can be used to access the SharePoint object model directly instead and in most cases achieve the same objectives, this isn’t as daunting as it sounds.


 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Configure the location for the output file
$Output="C:\amar\Output.csv";
"Site URL"+","+"Owner Login"+","+"Owner Email"+","+"Root Site Last Modified"+","+"Quota Limit (MB)"+","+"Total Storage Used (MB)"+","+"Site Quota Percentage Used" | Out-File -Encoding Default -FilePath $Output;
#Specify the root site collection within the Web app
$Siteurl="http://370dw7e07621";
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;
#Loops through each site collection within the Web app, if the owner has an e-mail address this is written to the output file
Foreach ($Site in $Webapp.Sites)
{
if ($Site.Quota.Storagemaximumlevel -gt 0)
{
 [int]$MaxStorage=$Site.Quota.StorageMaximumLevel /1MB
}
else {$MaxStorage="0"};
if ($Site.Usage.Storage -gt 0)
{
  [int]$StorageUsed=$Site.Usage.Storage /1MB
};
if ($Storageused-gt 0 -and $Maxstorage-gt 0)
{
  [int]$SiteQuotaUsed=$Storageused/$Maxstorage* 100}
else
{ $SiteQuotaUsed="0" };
$Web=$Site.Rootweb; $Site.Url + "," + $Site.Owner.Name + "," + $Site.Owner.Email + "," +$Web.LastItemModifiedDate.ToShortDateString() + "," +$MaxStorage+","+$StorageUsed + "," + $SiteQuotaUsed | Out-File -Encoding Default -Append -FilePath $Output;$Site.Dispose()};
 


No comments:

Post a Comment