SharePoint 2010 Search crawls can be scheduled to run Incremental or Full crawl pretty much any time you need. However, if you have more complex maintenance process, you probably want to run as a part of the batch when other tasks have completed.
In this post I will demonstrate how you can set the search crawl to run on multiple content sources within web search service application on demand.
We’ll start by creating an XML configuration file, let’s call it CrawlsConfig.xml with the following content which is pretty self explanatory:
<?xml version="1.0" encoding="UTF-8"?> <Setup SearchAppName="Search Service Application"> <ContentSources> <ContentSource CrawlType="Incremental">Local SharePoint sites</ContentSource> <ContentSource CrawlType="Full">Some other Content Source</ContentSource> </ContentSources> </Setup>
So there are two crawls defined here one incremental and one full.
Next is our script, let’s call it Crawl.ps1
function StartCrawls($contentsources, [string]$searchappname)
{
if($contentsources -ne $null)
{
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchappname
$csList = $contentsources.Item(0).SelectNodes("ContentSource")
foreach ($cs in $csList) {
Write-Host "-Starting crawl on: " $cs.InnerText
$contentsource = Get-SPEnterpriseSearchCrawlContentSource $cs.InnerText -SearchApplication $searchapp
$CrawlType = $cs.Attributes.Item(0).Value
Write-Host " Crawl type: " $CrawlType
if ($CrawlType -eq "Full") {
$contentsource.StartFullCrawl()
}
else {
$contentsource.StartIncrementalCrawl()
}
do {
$status = $contentsource.CrawlState;
Write-Host . -nonewline
Sleep -Milliseconds 1000
} while ($status -ne "Idle")
Write-Host
Write-Host "-Crawl complete"
Write-Host
}
}
}
[xml]$ConfigFile = Get-Content "CrawlsConfig.xml"
$SearchAppName = $ConfigFile.Setup.SearchAppName
# deploy solutions
StartCrawls $ConfigFile.SelectNodes("/Setup/ContentSources") $SearchAppName
Above, we open CrawlsConfig.XML file defined above, so make sure both files are in the same directory.
The we call the StartCrawls process which kicks off the crawl of the specified by and reports progress.
Ensure the Crawl.ps1 is ran from SharePoint Management Shell this way all of the SharePoint specific libraries are loaded.
If you’re into automation, check out how you can automate your solution deployments in my book
Enjoy!


