Thursday, May 22, 2014

PowerShell to remove "SiteOrphan"


Some more script to remove "SiteOrphan" error message from Health Analyzer Report.

Hope it can save you some time.

=================

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" 
cls

Set-Alias -Name stsadm -Value $env:CommonProgramFiles”\Microsoft Shared\Web Server Extensions\14\BIN\STSADM.EXE”

function RemoveOrphanedSites([string]$WebAppUrl, [switch]$ReportOnly)
{
$configDB = Get-SPDatabase | where {$_.typename -eq "Configuration Database"}
    $contentDBs = Get-SPWebApplication -IncludeCentralAdministration | Get-SPContentDatabase
    foreach($contentDB in $contentDBs)
    {
if ($WebAppUrl -ne "" -and $contentDB.WebApplication.Url.StartsWith($WebAppUrl) -eq $false)
{
continue
}

$webApplicationUrl = $contentDB.WebApplication.Url
Write-Host "webApplicationUrl=" $webApplicationUrl

[xml]$allwebs = stsadm -o enumallwebs -databasename $contentDB.Name
#$allwebs.OuterXML
$OrphanedSites = $allwebs.Databases.Database.site | ?{$_.insitemap -match "false"}
Foreach($site in $OrphanedSites)
{
if ($ReportOnly)
{
Write-Host "orphaned site, id=" $site.Id ", Url=" $site.Url ", not in sitemap"
}
else
{
Write-Host "orphaned site, id=" $site.Id ", Url=" $site.Url ", is removed from " $contentDB.Name 
$contentDB.ForceDeleteSite($site.Id, $false, $false)
}
}

}
}

Start-SPAssignment -Global

#RemoveOrphanedSites "http://spserver.local" –ReportOnly
RemoveOrphanedSites "http://spserver.local"

Stop-SPAssignment -Global

Write-Host "" 
Write-Host "Finished! Press enter key to exit." -ForegroundColor Green 
Read-Host 

references:

1. http://get-spscripts.com/2011/06/removing-features-from-content-database.html
2. http://sharepoint.stackexchange.com/questions/4752/removing-orphaned-sites

No comments:

Post a Comment