Tuesday, 29 May 2012

Force stop and then start a full crawl on all content sources in a SharePoint 2010 farm using PowerShell

There have been many times where I have needed to run a full crawl of all content sources on a SharePoint 2010 farm, but I quite often there are already crawls taking place, which I prefer to stop before starting a new one.

The script below walks through each content source and does the following:

  1. Checks whether the crawl status is set to “Idle”
  2. If the content source is currently involved in a crawl activity, stop the activity and wait until the status changes back to Idle
  3. If the content source is Idle, then start a full crawl

To use, run the following script in the SharePoint Management Shell, replacing “Search Service Application” with the name of your Search service application:

Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application" | ForEach-Object {
    if ($_.CrawlStatus -ne "Idle")
        Write-Host "Stopping currently running crawl for content source $($_.Name)..."
        do { Start-Sleep -Seconds 1 }
        while ($_.CrawlStatus -ne "Idle")
    Write-Host "Starting full crawl for content source $($_.Name)..."

For info, you can use the following script if you want to display the crawl status of all content sources on your farm:

Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application" | select Name, CrawlStatus

This will give you an output similar to the following:


Monday, 21 May 2012

Start all enabled timer jobs on a SharePoint 2010 farm using PowerShell and check their status

Starting a timer job is fairly easy with PowerShell on SharePoint 2010 using the Start-SPTimerJob command, but the script below walks through each timer job in the farm, checks that it is enabled for use (there’s not much point in trying to start a disabled timer job!), and then starts it:

Get-SPTimerJob | where { $_.IsDisabled -eq $false } | sort $_.Name | ForEach-Object {
        $timerJobName = $_.Name
        if ($_.WebApplication -ne $null) { $waMessage = "on web application $($_.WebApplication.Url)" }
        else { $waMessage = "on Farm" }
        Start-SPTimerJob -Identity $_
        Write-Host "Started timer job $timerJobName $waMessage"
        Write-Host "There was a problem starting timer job $timerjobName:" $_

This can be quite useful if you want to run everything in the farm before performing maintenance or as a test to ensure all timer jobs are operating successfully. Note that the script will report whether it is starting the timer job at the farm level or for each web application.

If you want to look at a report of the current status of each timer job, run the following script:

function Get-SPTimerJobStatus
    Get-SPTimerJob | sort Name | ForEach-Object {
        $lastRun = $_.HistoryEntries | Select-Object -first 1
        if ($_.WebApplication -eq $null) { $level = "Farm" }
        else { $level = $_.WebApplication.Url }
        $values = @{
            "Name" = $_.Name
            "Level" = $level
            "StartTime" = $lastRun.StartTime
            "EndTime" = $lastRun.EndTime
            "Status" = $lastRun.Status
        New-Object PSObject -Property $values | Select @("Name","Level","StartTime","EndTime","Status")
Get-SPTimerJobStatus | Out-GridView

This will produce a grid view showing each timer job, whether it was run at the farm or web application level, and the last run status, as shown in the example screenshot below: