August 28, 2013

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


   1:  #Import Localized Data
   2:  #Import-LocalizedData -BindingVariable Message
   3:  Add-PsSnapin Microsoft.SharePoint.PowerShell
   4:  #This function is used to get standard content database list for Get-OSCContentDatabase
   5:  function Export-OSCContentDatabase
   6:  {
   7:  PARAM
   8:      (   
   9:          $ContentDatabases
  10:      )
  11:          $ContentDatabases | Select-Object -Property `
  12:                              @{Name="Url";Expression={
  13:                                  $_.WebApplication.Url
  14:                                                     }},`
  15:                              @{Name="ID";Expression={
  16:                                   $_.Id
  17:                                                     }},`
  18:                              @{Name="Name";Expression={
  19:                                   $_.Name
  20:                                                      }},`
  21:                              @{Name="WebApplication";Expression={
  22:                                   $_.WebApplication
  23:                                                      }},`
  24:                              @{Name="Server";Expression={
  25:                                   $_.Server
  26:                                                      }},`
  27:                              @{Name="CurrentSiteCount";Expression={
  28:                                   $_.CurrentSiteCount
  29:                                                      }},`
  30:                              @{Name="Status";Expression={
  31:                                   $_.Status
  32:                                   }} | Sort-Object -Property Name
  33:  }
  34:   
  35:  function Get-OSCContentDatabase
  36:  {          
  37:      [CmdletBinding(DefaultParameterSetName="UnUsedDatabase")]
  38:      PARAM
  39:          (
  40:              [Parameter(Mandatory=$false,Position=0,ParameterSetName='UsedDatabase')]
  41:              [switch]$UsedDatabase,
  42:              [Parameter(Mandatory=$false,Position=0,ParameterSetName='UnUsedDatabase')]
  43:              [switch]$UnUsedDatabase
  44:           )
  45:              [array]$arrContentDB = @()
  46:              try
  47:                  {
  48:                       Get-SPWebApplication -IncludeCentralAdministration | ForEach-Object{
  49:                       $arrContentDB += $_.ContentDatabases
  50:                                  }
  51:                  }
  52:                  catch [Exception]
  53:                  {
  54:                       #Catch and throw the terminating exception
  55:                       throw $Error[0].Exception.Message
  56:                  }
  57:                 
  58:                  #Check if content databases exist
  59:                  if($arrContentDB.Count -eq 0)
  60:                  {
  61:                       Write-Error $Message.NoContentDB
  62:                       return $null
  63:                  }
  64:                 
  65:                  $scriptContentDBOutput = @()
  66:                  $scriptContentDBOutput += Export-OSCContentDatabase $arrContentDB
  67:                 
  68:                  #List the content databases which are in use currently
  69:                  if($UsedDatabase)
  70:                  {
  71:                        Write-Host $Message.UsedDatabase
  72:                        $scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -eq "Online"}
  73:                        if($scriptContentDBOutput.Count -eq 0)
  74:                         {
  75:                            Write-Host $Message.ZeroUsedContentDB
  76:                            return $null
  77:                         }
  78:                  }
  79:                  #List the content databases which are not in use currently
  80:                  elseif($UnUsedDatabase)
  81:                  {
  82:                          Write-Host $Message.UnUsedDatabase
  83:                          $scriptContentDBOutput = $scriptContentDBOutput | Where-Object{$_.Status -ne "Online"}
  84:                          if($scriptContentDBOutput.Count -eq 0)
  85:                              {
  86:                                Write-Host $Message.ZeroUnusedContentDB
  87:                                return $null
  88:                               }
  89:                  }
  90:                  #List all content databases
  91:                  else
  92:                  {
  93:                          Write-Host $Message.AllDatabase
  94:                          if($scriptContentDBOutput.Count -eq 0)
  95:                            {
  96:                                Write-Host $Message.ZeroContentDB
  97:                                return $null
  98:                            }
  99:                  }
 100:                 
 101:                  $scriptContentDBOutput
 102:  }
 103:   
 104:  ##Uncomment Following Line to List Out All Content Database
 105:  Get-OSCContentDatabase
 106:   
 107:  ##Uncomment Following Line to List the content databases which are in use currently.
 108:  Get-OSCContentDatabase -UsedDatabase
 109:   
 110:  ##Uncomment Following Line to List the content databases which are not in use currently.
 111:  Get-OSCContentDatabase -UnUsedDatabase

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment