Send-crawlhealth

===================================================================================
# Script: Send-CrawlHealth
# Desc: Get Crawl Health and send the resualt i a e-mail
# Returns: Array of ContentSources and health
# ===================================================================================

#region Assembly
[reflection.assembly]::LoadWithPartialName(“Microsoft.SharePoint”) | out-null
[reflection.assembly]::LoadWithPartialName(“Microsoft.Office.Server”) | out-null
[reflection.assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search”) | out-null
#endregion

#Region Threshholds
$crawlFreshnessDayThreshhold = 2
$crawlDurationHourThreshhold = 4
$successRatioThreshhold = 0.9
#endregion

#region Function Calculate-CrawlDuration
# ===================================================================================
# Func: Calculate-CrawlDuration
# Desc:
# Param: contentSource object
# ===================================================================================
function Calculate-CrawlDuration
{
param
([Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource)

if ($contentSource.CrawlStatus -eq [Microsoft.Office.Server.Search.Administration.CrawlStatus]::Idle)
{
return “OK – Idle”
}
# crawl running
$timespan = [datetime]::Now – $contentSource.CrawlStarted
$timespanFormatted = “Running for $($timespan.TotalDays.ToString(‘0.00’)) Days” +
“($($timespan.TotalHours.ToString(‘0.0’)) hours)”

if ($timespan.TotalHours -le ($crawlDurationHourThreshhold))
{
return “OK – $timespanFormatted”
} else {
return “Error – Threshhold exceeded – $timespanFormatted”
}
}
#endregion

#region Function Calculate-CrawlFreshness
# ===================================================================================
# Func: Calculate-CrawlFreshness
# Desc:
# Param: contentSource object
# ===================================================================================
function Calculate-CrawlFreshness
{
param
([Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource)

$timespan = [datetime]::Now – $contentSource.CrawlCompleted
$timespanFormatted = “$($timespan.TotalDays.ToString(‘0.00’)) days ago”
if ($timespan.Days -lt $crawlFreshnessDayThreshhold)
{
return “OK – $timespanFormatted”
}
else
{
return “Error – Threshhold exceeded – $timespanFormatted”
}
}
#endregion

#region Function Calculate-IndexHealth
# ===================================================================================
# Func: Calculate-IndexHealth
# Desc:
# Param: contentSource object
# ===================================================================================
function Calculate-IndexHealth
{
param
([Microsoft.Office.Server.Search.Administration.SharePointContentSource]$contentSource,
$successCount, $warningCount, $errorCount)

$formatted = “(Success:$($successCount)/Warnings:$($warningCount)/Error:$($errorCount))”
if ($errorCount -eq 1)
{
return “Error – exactly 1 error, usually indicates permissions/config error – $formatted”
}
$successRatio = ([double]$successCount)/([double]($warningCount + $errorCount))
if ($successRatio -ge $successRatioThreshhold)
{
return “OK – $formatted”
} else {
return “Error – Threshhold exceeded- $formatted”
}
}
#endregion

#region Function Get-CrawlHealth
# ===================================================================================
# Func: Get-CrawlHealth
# Desc: Get Crawl Health and return array with healthdata
# Returns: Array of ContentSources and health
# ===================================================================================
function Get-CrawlHealth
{
$serverContext = [Microsoft.Office.Server.ServerContext]::Default
$searchContext = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($serverContext)

$content = [Microsoft.Office.Server.Search.Administration.Content]$searchContext
$history = [Microsoft.Office.Server.Search.Administration.CrawlHistory]$searchContext

$contentSources = $content.ContentSources | foreach { $_ }

$contentSources | foreach {
#unroll DataTable object into more useful DataRow object
$crawlHistory = $history.GetLastCompletedCrawlHistory($_.Id) | % { $_ }
add-member -inputobject $_ -membertype NoteProperty -name “CurrentCrawlDuration” -value (
Calculate-CrawlDuration $_)
add-member -inputobject $_ -membertype NoteProperty -name “CompletedCrawlFreshness” -value (
Calculate-CrawlFreshness $_)
add-member -inputobject $_ -membertype NoteProperty -name “IndexHealth” -value (
Calculate-IndexHealth -contentSource $_ -successCount $crawlHistory.SuccessCount -warningCount (
$crawlHistory.WarningCount) -errorCount $crawlHistory.ErrorCount)
}

$contentSources | select Name, CurrentCrawlDuration, CompletedCrawlFreshness, IndexHealth
}
#endregion

# Send E-mail

$emailFrom = “from@yourdomain.com”
$emailTo = “to@yourdomain.com”
$subject = “CrawlHealth”
$body = Get-CrawlHealth | Format-List | Out-String
$smtpServer = “your smtp server”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: