Windows Search Server: initiate crawls by command line and scheduled task

article #965, updated 3091 days ago

The automatic crawl scheduler in Windows Search Server sometimes fails; you can get far more positive and creative control with scripting. Here’s how.

First you’ll need the name of the “Search Service Application”. I found these under “Farm Search Administration” in the SharePoint site; the default/built-in is named “Search Service Application”, but on the server being worked there was a second custom one for production work, which for this document we will call “SSAXYZ”.

Now we need the “Content Sources” list. Here’s where we learn whether it’s working or not.

Next step is to create the scripts below. You may want all of them. A usually working location and filename is in each initial comment. If your server doesn’t have an E:, you will need to rewrite just a tad.

The way it works is, you set your scheduled task to run the CMD, and the CMD starts up the PowerShell. It is possible to have the scheduled task run the PowerShell directly, but it’s much harder to read that way. There are two pairs of scripts below, one pair for incremental crawls, and one for full. It is often recommendable to run the incremental daily and the full weekly, if available server horsepower (most of all RAID) is enough for the fileset(s) involved.

The PowerShell comes from here, it stops any crawling which may be going on (or stalled) and restarts the desired (full or incremental), for every Search Service Application. So if you have multiple SSA’s you may want to modify so that it only does what you want.

REM E:\Search Server Scripts\Initiate-Full-Crawl.cmd
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  " & ' E:\Search Server Scripts\\Initiate-Full-Crawl.ps1 ' "
# E:\Search Server Scripts\Initiate-Full-Crawl.ps1
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "SSAXYZ" | ForEach-Object {
    if ($_.CrawlStatus -ne "Idle")
    {
        Write-Host "Stopping currently running crawl for content source $($_.Name)..."
        $_.StopCrawl()
        do { Start-Sleep -Seconds 1 }
        while ($_.CrawlStatus -ne "Idle")
    }
    Write-Host "Starting full crawl for content source $($_.Name)..."
    $_.StartFullCrawl()
}
REM E:\Search Server Scripts\Initiate-Incremental-Crawl.cmd
C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  " & ' E:\Search Server Scripts\\Initiate-Incremental-Crawl.ps1 ' "
# E:\Search Server Scripts\Initiate-Incremental-Crawl.ps1
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "SSAXYZ" | ForEach-Object {
    if ($_.CrawlStatus -ne "Idle")
    {
        Write-Host "Stopping currently running crawl for content source $($_.Name)..."
        $_.StopCrawl()        
        do { Start-Sleep -Seconds 1 }
        while ($_.CrawlStatus -ne "Idle")
    }
    Write-Host "Starting full crawl for content source $($_.Name)..."
    $_.StartIncrementalCrawl()
}

Categories: