Friday, May 11, 2012

SharePoint Content Databases Size - Storage Report

Every Month I've had to generate the Storage report for SharePoint Content Databases. How about automating it with PowerShell? Sure, Here is my code:

Get SharePoint Content Database Size using PowerShell:

#SharePoint Content Database Sizes - Storage Report 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$ReportDate=get-date -Format "MMM-dd-yyyy"

$OutputFN = "D:\Database_Storage_$ReportDate.csv"

#If the File already exists remove it
if (Test-Path "D:\Database_Storage_$ReportDate.csv")
{
    Remove-Item "D:\Database_Storage_$ReportDate.csv"
}

#Write CSV Header
 Add-Content -Path $OutputFN -Value "Web Application Name, URL, Database Name, Database Size-GB"
    
#Get All Web Applications
$webapps = Get-SPWebApplication
foreach($webapp in $webapps)
{
    $ContentDatabases = $webapp.ContentDatabases
    
    foreach($ContentDatabase in $ContentDatabases)
    {
        $ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2)

        Add-Content -Path $OutputFN " $($webapp.Name),  $($webapp.url) , $($ContentDatabase.Name) , $ContentDatabaseSize"
    }
} 

#Code To mail the Report
$SMTPClient = new-object System.Net.Mail.smtpClient
$SMTPClient.host = "smtp.crescent.org"

$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.Subject = "SharePoint Storage Report - $ReportDate"
$MailMessage.Body = "Please find attached Database storage report as on $ReportDate"
$MailMessage.From = "SharePoint-Reports@Crescent.com"
$MailMessage.To.add("Salaudeen.Rajack@Crescent.com")
$Attachment = new-object System.Net.Mail.Attachment($OutputFN)
$MailMessage.Attachments.Add($Attachment)
$SMTPClient.Send($MailMessage)

$Attachment.Dispose();
$MailMessage.Dispose();

#Remove the Report from Server once mailed!
Remove-Item "D:\Database_Storage_$ReportDate.csv"

Output: (After adding a Pivot Table)
sharepoint content database report

Tail: Once you have the script ready, You can place it under Task scheduler to run the script on scheduled basis! like: PowerShell.exe D:\Reports\StorageReport.ps1



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

2 comments :

  1. Hi, I am running this from my WFE and getting the email ok with attachment, but am getting an error message:
    "The term 'Get-SPWebApplication' is not recognized as the name of a cmdlet... ObjectNotFound: (Get-SPWebApplication:String)" in powershell. Do you have any suggestions?

    Running the cmd:
    PS D:\SPReports> .\contentdbstoragereport.ps1

    Thanks for looking

    ReplyDelete
    Replies
    1. Are you trying to Run the script in MOSS 2007? Get-SPWebApplication will work only on SharePoint 2010! To use it in MOSS, Take a Look at Gary's script: http://blog.falchionconsulting.com/index.php/2009/04/getting-an-spwebapplication-object-using-powershell/

      Delete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...