Wednesday, July 13, 2011

Schedule Crawl Start/Pause in SharePoint 2010 Search by PowerShell

In case of having not strong enough hardware there’s a pretty common request for start the crawl in the evening and pause in the next morning, before the work day starts. Scheduling the start of Full/Incremental Crawl is pretty easy from the admin UI, but you have to do some trick if you want to schedule the pause too. Here is my favorite trick: use PowerShell!
Here is what I do here:
  1. Create a script to start/resume the crawl (CrawlStart.ps1).
  2. Create a script to pause the crawl (CrawlPause.ps1).
  3. Schedule the script CrawlStart.ps1 to run in the evening (like 6pm).
  4. Schedule the script CrawlPause.ps1 to run in the morning (like 6am).
Is it simple, right? ;)

Here are some more details.
First, we have to know how to add the SharePoint SnapIn to PowerShell. Here is the command we need: Add-PSSnapin Microsoft.SharePoint.PowerShell.
Second, we have to get the Content Source from our Search Service Application:
$SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"
$ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source"

Then we have to know how to check the status of this content source’s crawl: $ContentSource.CrawlStatus. Here are the available values:
  • Idle
  • CrawlStarting
  • CrawlingIncremental / CrawlingFull
  • CrawlPausing
  • Paused
  • CrawlResuming
  • CrawlCompleting
  • CrawlStopping
Finally, we have to know how to start/pause/resume the crawling:
  • Start Full Crawl: $ContentSource.StartFullCrawl()
  • Start Incremental Crawl: $ContentSource.StartIncrementalCrawl()
  • Pause the current crawl: $ContentSource.PauseCrawl()
  • Resume the crawl: $ContentSource.ResumeCrawl()

That’s it. Here are the final scripts:
1. CrawlStart.ps1
Add-PSSnapin Microsoft.SharePoint.PowerShell
    $SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application" $ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source" if ($ContentSource.CrawlStatus  -eq "Idle" ) {         $ContentSource.StartIncrementalCrawl()     Write-Host "Starting Incremental Crawl" if ($ContentSource.CrawlStatus  -eq "Paused" ) {         $ContentSource.ResumeCrawl()     Write-Host "Resuminging Incremental Crawl" }
2. CrawlPause.ps1
Add-PSSnapin Microsoft.SharePoint.PowerShell
$SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"
$ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source"
Write-Host $ContentSource.CrawlState
if (($ContentSource.CrawlStatus  -eq "CrawlingIncremental" ) -or ($ContentSource.CrawlStatus  -eq "CrawlingFull" )) {
        $ContentSource.PauseCrawl()
    Write-Host "Pausing the current Crawl"
    }

Write-host $ContentSource.CrawlState
And finally, you have to schedule these tasks as a Windows job, by using these actions: powershell –command “& ‘C:\Scripts\CrawlStart.ps1’” to start and powershell –command “& ‘C:\Scripts\CrawlPause.ps1’” to pause your crawl.
Ps.: These scripts work fine for FAST Content Sources in SharePoint too, in this case you have to use the FAST Content SSA.

1 comment: