Here is what I do here:
- Create a script to start/resume the crawl (CrawlStart.ps1).
- Create a script to pause the crawl (CrawlPause.ps1).
- Schedule the script CrawlStart.ps1 to run in the evening (like 6pm).
- Schedule the script CrawlPause.ps1 to run in the morning (like 6am).
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"Then we have to know how to check the status of this content source’s crawl: $ContentSource.CrawlStatus. Here are the available values:
$ContentSource = $SSA | Get-SPEnterpriseSearchCrawlContentSource -Identity "My Content Source"
- Idle
- CrawlStarting
- CrawlingIncremental / CrawlingFull
- CrawlPausing
- Paused
- CrawlResuming
- CrawlCompleting
- CrawlStopping
- 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" }
Add-PSSnapin Microsoft.SharePoint.PowerShell
$SSA = Get-SPEnterpriseSearchServiceApplication -Identity "Search Service Application"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.
$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
Ps.: These scripts work fine for FAST Content Sources in SharePoint too, in this case you have to use the FAST Content SSA.
Nice work!! Very useful!!
ReplyDelete