Tuesday, January 15, 2013

Detect Content DB orphans and delete in a SharePoint 2010 Farm thru Windows PowerShell


Detect orphans in Content DB:

SCRIPT
----------------------------------------------------------------------------------------------------------------------------------
if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
     {Add-PSSnapin Microsoft.SharePoint.Powershell}
       set-location "C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\BIN"
         $SPFarm = Get-SPFarm
           $contentWebAppServices = (Get-SPFarm).services | ? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"} 

               foreach ($SPWebApp in $contentWebAppServices.WebApplications)
                 {
                   foreach ($SPSite in $SPWebApp.Sites)
                     {
                       Write-Host "Going thru Site Collection" $SPSite.Url
                         $SPSite.Url >> C:\SPSiteOrphanObject.txt
                           stsadm.exe -o databaserepair -url $SPSite.Url -databasename $SPSite.ContentDatabase.Name >> C:\SPSiteOrphanObject.txt
                             $SPSite.Dispose()
                               }}
                              ----------------------------------------------------------------------------------------------------------------------------------
                              • To delete orphan objects from a particular site use following script 
                              if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
                               {Add-PSSnapin Microsoft.SharePoint.Powershell}
                               set-location "C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\BIN"
                              stsadm.exe -o databaserepair -url -databasename  >> C:\SPSiteOrphansDelete.txt

                              Note
                              • You can get the Content DB of a particular site collection using below script
                              $site=Get-SPSite
                              write-host $site.ContentDatabase.Name
                              
                              
                              
                              
                              
                              

                              No comments: