At my current project where we’re upgrading from SharePoint 2007 to SharePoint 2013 we’re running into a high number of managed paths on a Web Application. The recommended limit according to TechNet is 20 but they’re well above that with around 70 managed paths. An internal study showed that a lot of them were Explicit Managed Paths where there was no real requirement to have it that way and they could easily be transformed into a (single) Wildcard Managed Path.
With this in mind the following PowerShell script automated this. It doesn’t ensure the Wildcard Managed Path since I wanted to control if the Explicit Managed Path should be transformed or not. In this case it will log a warning and skip that one.
### Script will loop explicit managed paths and restructure so the site collections are inside a wildcard managed path in order to reduce # (explicit) managed paths
cls
asnp Microsoft.SharePoint.PowerShell -ea 0 | Out-Null
$logFile = "C:\temp\managedpath_restructure.txt"
$trial = $false
$waUrl = "http://intranet"
$emps = Get-SPManagedPath -WebApplication $waUrl | ? { ($_.Type -eq "ExplicitInclusion") -and ($_.Name -like "*/*") }
$emps | % {
$emp = $_
$wmpName = $emp.Name.Split("/")[0]
$wmp = Get-SPManagedPath -WebApplication $waUrl | ? { ($_.Type -eq "WildcardInclusion") -and ($_.Name -eq $wmpName) }
if ($wmp -eq $null)
{
# SKIP
$log = [string]::Format("{0} - {1} - No wildcard MP '{2}'. Skipping.", (Get-Date), $emp.Name, $wmpName)
Write-Host -ForegroundColor Yellow $log
$log >> $logFile
}
else
{
# RESTRUCTURE
$log = [string]::Format("{0} - {1} - Restructuring explict MP to wildcard MP '{2}'.", (Get-Date), $emp.Name, $wmpName)
Write-Host -ForegroundColor Green $log
$log >> $logFile
# Get matching sites' content database
$cdbs = @()
Get-SPWebApplication $waUrl | Get-SPSite -Limit ALL | ? { $_.Url -like ("*/" + $emp.Name) } | % {
$s = $_
$cdb = $s.ContentDatabase
if (-not($cdbs.Contains($cdb)))
{
$cdbs += $cdb
}
$s.Close()
}
# Dismount content databases
$cdbs | % {
$cdb = $_
$log = [string]::Format("{0} - {1} - Dismounting content database '{2}'.", (Get-Date), $emp.Name, ($cdb.Name + " @ " + $cdb.Server))
Write-Host -ForegroundColor Green $log
$log >> $logFile
if (-not($trial))
{
Dismount-SPContentDatabase $cdb -Confirm:0
}
}
# Remove explicit managed patch
$log = [string]::Format("{0} - {1} - Removing explict MP.", (Get-Date), $emp.Name, $wmpName)
Write-Host -ForegroundColor Green $log
$log >> $logFile
if (-not($trial))
{
Remove-SPManagedPath $emp -WebApplication $waUrl -Confirm:0
}
# Mount content databases
$log = [string]::Format("{0} - {1} - Mounting content database '{2}'.", (Get-Date), $emp.Name, ($cdb.Name + ' @ ' + $cdb.Server))
Write-Host -ForegroundColor Green $log
$log >> $logFile
if (-not($trial))
{
$db = Mount-SPContentDatabase ($cdb.Name) -DatabaseServer ($cdb.Server) -WebApplication $waUrl -Confirm:0
}
}
}
Write-Host "DONE"