Export Search Service Application Crawler Settings to PowerShell in SharePoint 2010

If you ever wanted to take a snapshot of your search service application crawler settings in SharePoint 2010 then you already know that there’s no easy way of doing it without writing custom code. Even if you initially deployed crawler settings to your SharePoint farm using PowerShell scripts, chances are that some settings have been updated manually and the current configuration no longer matches the original deployment scripts. The following PowerShell script exports the main properties of Content Sources, Crawl Rules and Server Name Mappings that exist within a search service application. It’s not complete by any means but can serve as a starting point to be customized and extended to match your specific requirements. One thing to highlight is that the script doesn’t simply export the current search crawler settings to a data file but it actually translates your configuration into PowerShell commands that can later be executed to restore crawler settings to the current state.

$ssaName = "FASTContent"
$searchApp = Get-SPEnterpriseSearchServiceApplication $ssaName

$filePath = "ContentSourcesExport.ps1"
"`$searchApp = Get-SPEnterpriseSearchServiceApplication `"$ssaName`"" | Out-File $filePath
Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchApp | Foreach-Object {"New-SPEnterpriseSearchCrawlContentSource -SearchApplication `$searchApp -Name `"" + $_.Name + "`" -Type " + $_.Type + " -StartAddresses `"" + [System.String]::Join(",",$_.StartAddresses) + "`""} | Out-File $filePath -Append

$filePath = "CrawlRulesExport.ps1"
"`$searchApp = Get-SPEnterpriseSearchServiceApplication `"$ssaName`"" | Out-File $filePath
Get-SPEnterpriseSearchCrawlRule -SearchApplication $searchApp | Foreach-Object {"New-SPEnterpriseSearchCrawlRule -SearchApplication `$searchApp –Path `"" + $_.Path + "`" –CrawlAsHttp `$" + $_.CrawlAsHttp + " -Type " + $_.Type + " -FollowComplexUrls `$" + $_.FollowComplexUrls} | Out-File $filePath -Append

$filePath = "ServerNameMappingsExport.ps1"
"`$searchApp = Get-SPEnterpriseSearchServiceApplication `"$ssaName`"" | Out-File $filePath
Get-SPEnterpriseSearchCrawlMapping -SearchApplication $searchApp | Foreach-Object {"New-SPEnterpriseSearchCrawlMapping -SearchApplication `$searchApp -Url `"" + $_.Source + "`" -Target `"" + $_.Target + "`""} | Out-File $filePath -Append

The script below can be used to easily delete all of Content Sources, Crawl Rules and Server Name Mappings that exist within a given search service application.

$ssaName = "FASTContent"
$searchApp = Get-SPEnterpriseSearchServiceApplication $ssaName

Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchApp | Remove-SPEnterpriseSearchCrawlContentSource

Get-SPEnterpriseSearchCrawlRule -SearchApplication $searchApp | Remove-SPEnterpriseSearchCrawlRule

Get-SPEnterpriseSearchCrawlMapping -SearchApplication $searchApp | Remove-SPEnterpriseSearchCrawlMapping

Revert customized master pages back to ghosted state in SharePoint 2010

When you first deploy master pages and page templates as part of a SharePoint 2010 solution package and then provision the deployed files to the master page gallery using a feature, the provisioned files start in a ghosted state and get automatically updated on every package deployment. What happens if someone makes manual changes to the provisioned files in SharePoint Designer or by uploading an updated version of the file to the master page gallery is that the edited pages get disconnected from the page version that is deployed to the file system by the SharePoint solution package and become customized, or unghosted. Any page changes deployed as part of the SharePoint solution package after that aren’t going to be reflected on the site until the pages are uncustomized and return back to the ghosted state. The process of reverting the customized files back to the ghosted state using the SharePoint UI usually involves deleting the file from the master page gallery and then reactivating the feature to provision the deployed file. This solution is not always possible or easy to do as there may be many pages that depend on the customized page template or sites using the customized master page and SharePoint UI won’t allow you to delete the file from the gallery if that’s the case. Luckily, there’s a simple way to accomplish the desired effect in PowerShell!

First, get a reference to the root SPWeb object for the site collection and check the customization status of the page:

Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$web = Get-SPWeb http://intranet.contoso.com
$file = $web.GetFile("/_catalogs/masterpage/MyCustomMasterPage.master")

The possible page customization options are:

  • Uncustomized – the page is not customized and is in the ghosted state. No action is necessary.
  • Customized – the page has been customized and is currently in the unghosted state. This page can be reverted back to the ghosted state using the script below.
  • None – the page was never ghosted. This is often the case if the master page was uploaded to the master page gallery first and only then added to the SharePoint deployment package. The only solution here is to delete the file and reactivate the feature to provision the deployed version of the page.

The following script will revert any customized page back to the ghosted state:

if($file.CustomizedPageStatus -eq "Customized") {

At this point the page customization status will change to Uncustomized, the page will return back to the ghosted state and will be in sync with the page version deployed as part of the SharePoint deployment package.