How to: Programmatically Manage the Crawl of a Content Source in PowerShell
Hi,
If you have read my previous post you may think, why did you stop there? Well, that is what I thought too :), and started with this article from MSDN.
We wanted to start managing the crawling of our content source more programmatically, as we have seen that running several at the same time affects the overall process.
So running these powershell scripts as scheduled task and monitoring the status can improve in crawling.
You may improve them reusing the context and content sources and doing some pipeline
to be included in the overall file:
## SharePoint Reference
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search.Administration")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
To start an incremental crawl of the content source
function global:StartIncremental-Crawl($url, $csname)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);$cs = $sc.ContentSources[$csname];
$cs.StartIncrementalCrawl();
$s.Dispose();
}StartIncremental-Crawl -url https://your_site_url -csname "your content source name"
To start a full crawl of the content source
function global:StartFull-Crawl($url, $csname)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);$cs = $sc.ContentSources[$csname];
$cs.StartFullCrawl();
$s.Dispose();
}StartFull-Crawl -url https://your_site_url -csname "your content source name"
To pause a crawl in process
function global:Pause-Crawl($url, $csname)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);$cs = $sc.ContentSources[$csname];
$cs.PauseCrawl();
$s.Dispose();
}Pause-Crawl -url https://your_site_url -csname "your content source name"
To resume a paused crawl
function global:Resume-Crawl($url, $csname)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);$cs = $sc.ContentSources[$csname];
$cs.ResumeCrawl();
$s.Dispose();
}Resume-Crawl -url https://your_site_url -csname "your content source name"
To stop a crawl of the content source
function global:Stop-Crawl($url, $csname)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);$cs = $sc.ContentSources[$csname];
$cs.StopCrawl();
$s.Dispose();
}Stop-Crawl -url https://your_site_url -csname "your content source name"
To check the crawl status values for a content source
function global:Get-CrawlStatus($url)
{
trap [Exception] {
write-error $("ERROR: " + $_.Exception.GetType().FullName);
write-error $("ERROR: " + $_.Exception.Message);
continue;
}$s = new-Object Microsoft.SharePoint.SPSite($url);
$c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
$sc = new-Object Microsoft.Office.Server.Search.Administration.Content($c);Write-Output $sc.ContentSources;
$s.Dispose();
}Get-CrawlStatus -url https://your_site_url | Format-Table -property CrawlStatus, CrawlStarted, CrawlCompleted
Enjoy!
Comments
- Anonymous
December 11, 2009
Is there a way to list all content sources that are crawling or paused so I can itterate through them instead of specifying them? - Anonymous
January 02, 2014
Checkout this link for enterprise / fast search content source update through c# codeblog.techperspect.com/.../how-to-programmatically-update-content.html