Powershell Script Block To List ALL/Unused/Used Content Database

===================================================
clear
#Import Localized Data
#Import-LocalizedData -BindingVariable Message
Add-PsSnapin Microsoft.SharePoint.PowerShell
#This function is used to get standard content database list for Get-OSCContentDatabase
function Export-OSCContentDatabase
{
PARAM
(
$ContentDatabases
)

$ContentDatabases | Select-Object -Property `
@{Name=”Url”;Expression={
$_.WebApplication.Url
}},`
@{Name=”ID”;Expression={
$_.Id
}},`
@{Name=”Name”;Expression={
$_.Name
}},`
@{Name=”WebApplication”;Expression={
$_.WebApplication
}},`
@{Name=”Server”;Expression={
$_.Server
}},`
@{Name=”CurrentSiteCount”;Expression={
$_.CurrentSiteCount
}},`
@{Name=”Status”;Expression={
$_.Status
}} | Sort-Object -Property Name
}

function Get-OSCContentDatabase
{
<#
.SYNOPSIS
Get-OSCContentDatabase is an advanced function which can be used to get unused,used or all content databases.
.DESCRIPTION
Get-OSCContentDatabase is an advanced function which can be used to get unused,used or all content databases.
.PARAMETER <UsedDatabase>
List the content databases which are in use currently.
.PARAMETER <UnUsedDatabase>
List the content databases which are not in use currently.
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase
List all content databases.
Url : http://testserver:5000/
ID : caecf4a5-a618-447f-8d82-91a39bc45956
Name : TestContentDB1
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Online
Url : http://testserver:5000/
ID : 5361df6c-a487-4f0f-ba26-52a82dba75a6
Name : TestContentDB2
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
Url : http://testserver:21379/
ID : dca43cae-3dc5-4fd3-9ee6-90e1686514b3
Name : TestContentDB3
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 2
Status : Online
Url : http://testserver:21379/
ID : 55e0878d-2862-4f26-9481-c07aa012f9fa
Name : TestContentDB4
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase -UsedDatabase
List the content databases which are in use currently.
Url : http://testserver:5000/
ID : caecf4a5-a618-447f-8d82-91a39bc45956
Name : TestContentDB1
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Online
Url : http://testserver:21379/
ID : dca43cae-3dc5-4fd3-9ee6-90e1686514b3
Name : TestContentDB3
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 2
Status : Online
.EXAMPLE
PS C:\Users\TestUser> Get-OSCContentDatabase -UnUsedDatabase
List the content databases which are not in use currently.
Url : http://testserver:5000/
ID : 5361df6c-a487-4f0f-ba26-52a82dba75a6
Name : TestContentDB2
WebApplication : SPWebApplication Name=ClaimWebApp – 5000
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
Url : http://testserver:21379/
ID : 55e0878d-2862-4f26-9481-c07aa012f9fa
Name : TestContentDB4
WebApplication : SPAdministrationWebApplication
Server : TestServer\SharePoint
CurrentSiteCount : 0
Status : Disabled
#>

[CmdletBinding(DefaultParameterSetName=”UnUsedDatabase”)]
PARAM
(
[Parameter(Mandatory=$false,Position=0,ParameterSetName=’UsedDatabase’)]
[switch]$UsedDatabase,
[Parameter(Mandatory=$false,Position=0,ParameterSetName=’UnUsedDatabase’)]
[switch]$UnUsedDatabase
)

[array]$arrContentDB = @()
try
{
Get-SPWebApplication -IncludeCentralAdministration | ForEach-Object{
$arrContentDB += $_.ContentDatabases
}
}
catch [Exception]
{
#Catch and throw the terminating exception
throw $Error[0].Exception.Message
}

#Check if content databases exist
if($arrContentDB.Count -eq 0)
{
Write-Error $Message.NoContentDB
return $null
}

$scriptContentDBOutput = @()
$scriptContentDBOutput += Export-OSCContentDatabase $arrContentDB

#List the content databases which are in use currently
if($UsedDatabase)
{
Write-Host $Message.UsedDatabase
$scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -eq “Online”}
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroUsedContentDB
return $null
}
}
#List the content databases which are not in use currently
elseif($UnUsedDatabase)
{
Write-Host $Message.UnUsedDatabase
$scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -ne “Online”}
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroUnusedContentDB
return $null
}
}
#List all content databases
else
{
Write-Host $Message.AllDatabase
if($scriptContentDBOutput.Count -eq 0)
{
Write-Host $Message.ZeroContentDB
return $null
}
}

$scriptContentDBOutput
}

##List Out All Content Database
Get-OSCContentDatabase
##List the content databases which are in use currently.
Get-OSCContentDatabase -UsedDatabase
##List the content databases which are not in use currently.
Get-OSCContentDatabase -UnUsedDatabase
=================================================
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s