SharePoint Content Database
A content database is a database file that stores content for one or more site collections for SharePoint web application. The content can be pages, files, documents, images and much more. So if the Site Collection has more number of SharePoint sites, the content database size grows rapidly.
The script gets you the below details about the SharePoint content database,
- Content database located server
- CDB status
- CDB size
- Site Level warning
- Maximum allowed sites
- Total site collection
- Site collection URL
- Subsite count
- Site collection size
Below piece of code generates the above said information’s,
- Function ContentDatabaseReport()
- {
-
- $CDBName = read-host "Enter the content database name "
- write-host "Generating report for the Content database " $CDBName -fore yellow
- write-host "Processing report..................." -fore magenta
- $Output = $scriptBase + "\" + "03ContentDBDetails.csv";
- "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;
- $CDB = get-spcontentdatabase -identity $CDBName
- $CDB.name + "," + $CDB.server + "," + $CDB.Status + "," + $CDB.DiskSizeRequired/1048576 + "," + $CDB.WarningSiteCount + "," + $CDB.MaximumSiteCount + "," + $CDB.Currentsitecount + "," + $empty + "," + $empty + "," + $empty | Out-File -Encoding Default -Append -FilePath $Output;
- $sites = get-spsite -limit all -ContentDatabase $CDBName
- foreach($site in $sites)
- {
- $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default -Append -FilePath $Output;
- }
- write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green
-
- }
Complete Code - $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
- $LogFile = ".\ContentDatabaseReportPatch-$LogTime.rtf"
-
- # Add SharePoint PowerShell Snapin
-
-
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
-
- $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
- Set-Location $scriptBase
-
-
- #Deleting any .rtf files in the scriptbase location
- $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
- if($FindRTFFile)
- {
- foreach($file in $FindRTFFile)
- {
- remove-item $file
- }
- }
-
-
- start-transcript $logfile
-
- Function ContentDatabaseReport()
- {
-
- $CDBName = read-host "Enter the content database name "
- write-host "Generating report for the Content database " $CDBName -fore yellow
- write-host "Processing report..................." -fore magenta
- $Output = $scriptBase + "\" + "03ContentDBDetails.csv";
- "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;
- $CDB = get-spcontentdatabase -identity $CDBName
- $CDB.name + "," + $CDB.server + "," + $CDB.Status + "," + $CDB.DiskSizeRequired/1048576 + "," + $CDB.WarningSiteCount + "," + $CDB.MaximumSiteCount + "," + $CDB.Currentsitecount + "," + $empty + "," + $empty + "," + $empty | Out-File -Encoding Default -Append -FilePath $Output;
- $sites = get-spsite -limit all -ContentDatabase $CDBName
- foreach($site in $sites)
- {
- $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default -Append -FilePath $Output;
- }
- write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green
-
- }
-
- ContentDatabaseReport
-
- write-host ""
- write-host "SCRIPT COMPLETED" -fore green
-
- stop-transcript
Execution Steps- Download and copy the script to the SharePoint server
- Launch the SharePoint management shell
- Navigate to the script path and execute the script,
Conclusion
Thus this article explains on how to get report of a SharePoint content database using powershell script.