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
{
         
    [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
}
 
##Uncomment Following Line to List Out All Content Database
#Get-OSCContentDatabase
 
##Uncomment Following Line to List the content databases which are in use currently.
Get-OSCContentDatabase -UsedDatabase
 
##Uncomment Following Line to List the content databases which are not in use currently.
#Get-OSCContentDatabase -UnUsedDatabase