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.
- $snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
- if ($snapin -eq $null) {
- Write-Host "[INIT] Loading SharePoint Powershell Snapin"
- Add-PSSnapin "Microsoft.SharePoint.Powershell"
- }
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.
- #Get all content sources and their type, crawl status and last Crawl completed.
- Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application"
Then next, from user input, Content Source ID is saved.
- #Get user input on Content Source
- $ContentSource = Read-Host -Prompt 'Input your Content Source
Below code will display the content source which we have selected for confirmation
- $searchApp =
- Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application" |
- Where-Object { $_.ID -eq "$ContentSource" }
-
- #Display the selected content Source
- Write-Host 'You have selected the content Source:' $searchApp.Name -ForegroundColor Yellow
Below condition will check if the Crawl status is Idle or not.
- #Condition to check if the crawl is Idle or running.
- if($searchApp.CrawlState -eq "Idle") {
- Write-Host "CrawlState was Idle, Last crawling Completed " -ForegroundColor Green
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.
- #Choice to select Incremental or Full Crawl
- $crawlType = Read-Host -Prompt 'Input your Crawl Preference (1.Incremental,2. Full)'
-
- if($crawlType -eq 1) {
-
- Write-Host "You have selected Incremental Crawl."
-
- #Command to start Incremntal Crawl for the selected content source
- $searchApp.StartIncrementalCrawl()
- }
-
- if($crawlType -eq 2) {
-
- Write-Host "You have selected Full Crawl."
-
- #Command to start Full Crawl for the selected content source
- $searchApp.StartFullCrawl()
- }
-
- Write-Host "Crawl Started at" $searchApp.CrawlStarted -ForegroundColor Yellow
-
- Do {
- Write-Host "`r#" -ForegroundColor Yellow -NoNewline
- Start-Sleep 5
- } 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.
- #Display the latest crawl completed date and Time, Sucesses, Warnings, Errors, Deletes.
- Write-Host "`nFinsihed crawling" $searchApp.Name "at" $searchApp.CrawlCompleted -ForegroundColor Green
- Write-Host "Crawl Successes Count:" $searchApp.SuccessCount -ForegroundColor Green
- Write-Host "Crawl Warning Count:" $searchApp.WarningCount -ForegroundColor Yellow
- Write-Host "Crawl Error Count:" $searchApp.ErrorCount -ForegroundColor Red
- Write-Host "Crawl Delete Count:" $searchApp.DeleteCount -ForegroundColor Yellow
-
-
- } else{
- Write-Host "Didn't start crawl, CrawlState was"$searchApp.CrawlState -ForegroundColor Yellow
- }
Conculsion
Thus, you learned how to create content source in SharePoint and automate the crawl using PowerShell script.