PoshCode Logo PowerShell Code Repository

Get-CrawlHealth (MOSS) (modification of post by Peter view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1264"></script>download | new post

This extends the functionality of the SharePoint Search Administration page, producing dashboard-like analysis of your farm’s search crawls. I do not intend to use this script in production, but instead will use it as a starting point to help me build out search crawl health monitoring functionality.

Added mid-expression newlines for readability. EDIT: now no longer formats the result as a list—you may now choose to format the results yourself (or not)
EDIT: oopsies, fixed math error in index health calculator; now index health calculator is (appropriately) less generous

  1. [reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null
  2. [reflection.assembly]::LoadWithPartialName("Microsoft.Office.Server") | out-null
  3. [reflection.assembly]::LoadWithPartialName("Microsoft.Office.Server.Search") | out-null
  4.  
  5. #NOTE: I've set strict crawl freshness/crawl duration/success ratio threshholds. Reset as desired
  6. #      to something that more suits your reality.
  7. $crawlFreshnessDayThreshhold = 2
  8. $crawlDurationHourThreshhold = 4
  9. $successRatioThreshhold = 0.9
  10.  
  11.  
  12. function Calculate-CrawlDuration(
  13.         [Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource)
  14. {
  15.         if ($contentSource.CrawlStatus -eq [Microsoft.Office.Server.Search.Administration.CrawlStatus]::Idle) {
  16.                 return "Green - no current crawl"
  17.         }
  18.        
  19.         $timespan = [datetime]::Now - $contentSource.CrawlStarted
  20.         $timespanFormatted = "Running for $($timespan.TotalDays.ToString('0.00')) Days" +
  21.                 "($($timespan.TotalHours.ToString('0.0')) hours)"
  22.        
  23.         if ($timespan.TotalHours -le ($crawlDurationHourThreshhold / 2)) {
  24.                 return "Green - $timespanFormatted"
  25.         } elseif ($timespan.TotalHours -le ($crawlDurationHourThreshhold)) {
  26.                 return "Yellow - $timespanFormatted"
  27.         } else {
  28.                 return "Red - $timespanFormatted"
  29.         }
  30. }
  31.  
  32.  
  33. function Calculate-CrawlFreshness(
  34.         [Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource)
  35. {
  36.         $timespan = [datetime]::Now - $contentSource.CrawlCompleted
  37.         $timespanFormatted = "$($timespan.TotalDays.ToString('0.00')) days ago"
  38.         if ($timespan.Days -le 0) {
  39.                 return "Green - $timespanFormatted"
  40.         } elseif ($timespan.Days -lt $crawlFreshnessDayThreshhold) {
  41.                 return "Yellow - $timespanFormatted"
  42.         } else {
  43.                 return "Red - $timespanFormatted"
  44.         }
  45. }
  46.  
  47.  
  48. function Calculate-IndexHealth(
  49.         [Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource,
  50.         $successCount, $warningCount, $errorCount)
  51. {
  52.         $formatted = "($($successCount)/$($warningCount)/$($errorCount))"
  53.         if ($errorCount -eq 1) {
  54.                 return "Red - exactly 1 error, usually indicates permissions/config error - $formatted"
  55.         }
  56.        
  57.         $successRatio = ([double]$successCount)/([double]($warningCount + $errorCount))
  58.         $successRatioMidpointToPerfection = (1.0 + $successRatioThreshhold)/2.0
  59.         if ($successRatio -ge $successRatioMidpointToPerfection) {
  60.                 return "Green - $formatted"
  61.         } elseif ($successRatio -ge $successRatioThreshhold) {
  62.                 return "Yellow - $formatted"
  63.         } else {
  64.                 return "Red - $formatted"
  65.         }
  66. }
  67.  
  68. function Get-CrawlHealth
  69. {
  70.         $serverContext = [Microsoft.Office.Server.ServerContext]::Default
  71.         $searchContext = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($serverContext)
  72.  
  73.         $content = [Microsoft.Office.Server.Search.Administration.Content]$searchContext
  74.         $history = [Microsoft.Office.Server.Search.Administration.CrawlHistory]$searchContext
  75.        
  76.         $contentSources = $content.ContentSources | foreach { $_ }
  77.        
  78.         $contentSources | foreach {
  79.                 #unroll DataTable object into more useful DataRow object
  80.                 $crawlHistory = $history.GetLastCompletedCrawlHistory($_.Id) | % { $_ }
  81.                 add-member -inputobject $_ -membertype NoteProperty -name "CurrentCrawlDuration" -value (
  82.                         Calculate-CrawlDuration $_)
  83.                 add-member -inputobject $_ -membertype NoteProperty -name "CompletedCrawlFreshness" -value (
  84.                         Calculate-CrawlFreshness $_)
  85.                 add-member -inputobject $_ -membertype NoteProperty -name "IndexHealth" -value (
  86.                         Calculate-IndexHealth -contentSource $_ -successCount $crawlHistory.SuccessCount -warningCount (
  87.                                 $crawlHistory.WarningCount) -errorCount $crawlHistory.ErrorCount)
  88.         }
  89.        
  90.         $contentSources | select Name, CurrentCrawlDuration, CompletedCrawlFreshness, IndexHealth
  91. }
  92.  
  93.  
  94.  
  95. #USAGE: -Open a PowerShell session on the SharePoint server with elevated credentials
  96. #       (specifically, with access to the SSP - usually the SharePoint farm account)
  97. #       -Tweak the threshholds (they may be too ambitious for your environment)
  98. #        -Paste this text into an open PowerShell window and type (without the # mark)
  99. #        Get-CrawlHealth | Format-List

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me