How to Start a SharePoint 2010 / FS4SP Crawl Remotely

With SharePoint 2010 and FAST Search For SharePoint 2010 (FS4SP), it's easy to schedule crawls to run daily, hourly, or according to any other frequency. For most scenarios, scheduled crawls work perfectly.

Sometimes it makes more sense to kick off a crawl based on an event. For example, perhaps your organization runs an Extract/Transform/Load process to prepare data before being crawled. If that ETL job finishes at an inconsistent time, a scheduled crawl may either run too early and miss some updated data or run too late, making queries stale.

To fix that, we'd ideally kick off a new search crawl as soon as the ETL job is done running. With PowerShell, doing so is easy.

The PowerShell Script

$userName = "DOMAIN\serviceAccount"
$passWord = ConvertTo-SecureString "password" -Force -AsPlainText
$indexServerName = “serverName”

# Run the following commands on the remote computer
$credential = New-Object System.Management.Automation.PSCredential($userName, $passWord)
$session = New-PSSession $indexServerName -Authentication CredSSP -Credential $credential
Invoke-Command -Session $session -scriptBlock { `
Add-PSSnapin Microsoft.SharePoint.PowerShell; `
`
$indexServiceAppName = “Search Service Index Application”; `
`
$indexServiceApp = Get-SPServiceApplication -Name $indexServiceAppName; `
$contentSource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $indexServiceApp `
$contentSource.StartFullCrawl() `
}

How It Works

The above script uses PowerShell remoting to issue requests on a SharePoint indexing server.

The following variables need to filled in:

  • $userName: The full username of an account with permissions to kick off a new search.
  • $passWord: The account's password. Note that dollar signs need to be escaped with tick characters in PowerShell strings (e.g. "Pa`$`$word").
  • $indexServerName: The name of a server running the index role.

An example usage is to run this script as part of a SQL job or SSIS step. The executable to call is "PowerShell.exe" with the above script saved in a "PS1" file as the command's argument.

Because SharePoint 2010 and FAST Search for SharePoint use the same service application architecture, this approach works for either system.