Automation Of SharePoint Search Crawl Using PowerShell Scripts - Part Two

Introduction

In the previous part of this series, you have learned how to create content source and select the appropriate option for each category. In this continuation, you will understand how to create PowerShell scripts to automate Crawls.

We can create Schedules to run at specific intervals using schedules, but what if you want to create Automated PowerShell script for these content sources using Crawling? Here is the detailed answer.

Description

As the first step, open PowerShell ISE as Administrator and add PSSnapin for Microsoft.SharePoint.PowerShell if it's not added already. 

The below script will check if the Microsoft.SharePoint.PowerShell is added or not, if not this will do it.

  1. $snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }  
  2. if ($snapin -eq $null) {  
  3.     Write-Host "[INIT] Loading SharePoint Powershell Snapin"  
  4.     Add-PSSnapin "Microsoft.SharePoint.Powershell"  
  5. }  

Next, you have to select the search application that is running in the farm.

Get-SPEnterpriseSearchCrawlContentSource and the cmdlet will get all the search application content source available, but you need to provide Search Application name.

  1. #Get all content sources and their type, crawl status and last Crawl completed.  
  2. Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application"  

SharePoint Search Crawl

Then next, from user input, Content Source ID is saved.

  1. #Get user input on Content Source  
  2. $ContentSource = Read-Host -Prompt 'Input your Content Source   

Below code will display the content source which we have selected for confirmation

  1. $searchApp =     
  2.     Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application" |     
  3.     Where-Object { $_.ID -eq "$ContentSource" }    
  4.   
  5. #Display the selected content Source   
  6. Write-Host 'You have selected the content Source:' $searchApp.Name -ForegroundColor Yellow  

SharePoint Search Crawl

Below condition will check if the Crawl status is Idle or not. 

  1. #Condition to check if the crawl is Idle or running.   
  2. if($searchApp.CrawlState -eq "Idle") {  
  3.     Write-Host "CrawlState was Idle, Last crawling Completed " -ForegroundColor Green  

SharePoint Search Crawl

This below code will get input from user whether you want to run Incremental Crawl or Full Cr l, and then based on the given content source and crawl type, it will run the crawl.

  1. #Choice to select Incremental or Full Crawl  
  2. $crawlType = Read-Host -Prompt 'Input your Crawl Preference (1.Incremental,2. Full)'  
  3.   
  4. if($crawlType -eq 1)  {  
  5.   
  6. Write-Host "You have selected Incremental Crawl."  
  7.  
  8. #Command to start Incremntal Crawl for the selected content source  
  9. $searchApp.StartIncrementalCrawl()  
  10. }  
  11.   
  12. if($crawlType -eq 2)  {  
  13.   
  14. Write-Host "You have selected Full Crawl."  
  15.  
  16. #Command to start Full Crawl for the selected content source  
  17. $searchApp.StartFullCrawl()  
  18. }  
  19.   
  20. Write-Host "Crawl Started at" $searchApp.CrawlStarted -ForegroundColor Yellow  
  21.   
  22. Do {  
  23.     Write-Host "`r#" -ForegroundColor Yellow -NoNewline  
  24.     Start-Sleep 5  
  25. while ($searchApp.CrawlState -ne "Idle")  

At last, when it comes to finishing the crawl, the script will display the statistics for the selected content sources.

  1.     #Display the latest crawl completed date and Time, Sucesses, Warnings, Errors, Deletes.  
  2.     Write-Host "`nFinsihed crawling" $searchApp.Name "at" $searchApp.CrawlCompleted -ForegroundColor Green      
  3.     Write-Host "Crawl Successes Count:" $searchApp.SuccessCount  -ForegroundColor Green  
  4.     Write-Host "Crawl Warning Count:" $searchApp.WarningCount -ForegroundColor Yellow  
  5.     Write-Host "Crawl Error Count:" $searchApp.ErrorCount -ForegroundColor Red  
  6.     Write-Host "Crawl Delete Count:" $searchApp.DeleteCount -ForegroundColor Yellow  
  7.       
  8.    
  9. else{  
  10.     Write-Host "Didn't start crawl, CrawlState was"$searchApp.CrawlState -ForegroundColor Yellow  
  11. }  

SharePoint Search Crawl

Conculsion

Thus, you learned how to create content source in SharePoint and automate the crawl using PowerShell script.