PowerShell: Repairing SharePoint Content Databases

Introduction

When you get a warning from SharePoint saying that a database has orphaned content, the message is not the most helpful.

This is an example of health analyser message in SharePoint Central Admin

orphaneditemshealthcheckerror

There is no information on which content database is affected, although you can click on the “Repair Automatically” option but you don’t have any context as to which database is affected. This is going to be a problem if we need to repair the database via a documented change process.

The following process will show you how to identify the item and database that is affected.

Start by running the SharePoint Management Shell.

Get-SPContentDatabase | %{Write-Host $_; $_.Repair($false);}

This will return something like below:-

Database Name 1
<OrphanedObjects Count="0" />
Database Name 2
<OrphanedObjects Count="0" />
Database Name 3
<OrphanedObjects Count="0" />
Database Name 4
<OrphanedObjects Count="0" />
Database Name 4
<OrphanedObjects Count="0" />
Database Name 5
<OrphanedObjects Count="0" />
Database Name 6
<OrphanedObjects Count="0" />
Database Name 7
<OrphanedObjects Count="0" />
Database Name 8
<OrphanedObjects Count="1">
 <Orphan Type="SecurityScope" SiteId="{166BF298-DE66-4919-A506-4F3412E8A86E}"
Name="sites/test/SiteCollectionImages/Forms/Video/docsethomepage.aspx" InRecycleBin="Yes" />
</OrphanedObjects>

Now we can see that there is some orphaned content in content database “Database Name 8”.

To fix the database, run the following:-

$repairdb = Get-SPContentDatabase -Identity "[Database Name]";

Check that the database is the right one by outputting the $repairdb object.

To repair the database run the following command.

$repairdb.Repair($true)
<OrphanedObjects Count="1">
 <Orphan Type="SecurityScope" SiteId="{166BF298-DE66-4919-A506-4F3412E8A86E}"
Name="sites/test/SiteCollectionImages/Forms/Video/docsethomepage.aspx" InRecycleBin="Yes" />
</OrphanedObjects>

Now let’s check that the corruption is gone by re-running the command

Get-SPContentDatabase | %{Write-Host $_; $_.Repair($false);}

We should see that there are no more corruptions

Now go into Central Admin site and re-run the failed health rule, to resolve the issue.

Thoughts? Comments about this post? Please leave them here..

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