1

I could able to get the name of active crawls, how to get the current crawl duration

From the below powershell script which i tried.

Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application DEV" | ForEach-Object {
if ($_.CrawlStatus -ne "Idle")
{
    Write-Host $_.Name      

}  

}

Current crawl duration

1

The duration is not exposed, however the start date/time is available under the crawl source property "CrawlStarted". With this, we can use the Get-Date cmdlet to get the current date/time and subtract the CrawlStarted date/time to find the duration. The following PowerShell can be used to print the duration to the console.

Add-PSSnapin microsoft.sharepoint.powershell

$sources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service"
ForEach ($source in $sources) {
    if ($source.CrawlStatus -ne "Idle") {
        Write-Host ((Get-Date) - $source.CrawlStarted)
    }
}

To address your question of finding previous crawl logs and sending an alert if the crawl took over 3 hours, please try the script below. It will write an alert to the console if the last crawl for any content source took over 3 hours. Please modify to suit your exact requirements.

Add-PSSnapin microsoft.sharepoint.powershell

$ssa = Get-SPEnterpriseSearchServiceApplication | Where-Object {
    $_.Name -eq "Search Service"
}
$sources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa
ForEach ($source in $sources) {
    $log = New-Object Microsoft.Office.Server.Search.Administration.CrawlLog $ssa
    $last = $log.GetCrawlHistory(1, $source.Id)[0]
    if ($last.CrawlDuration.Hours -ge 3) {
        Write-Host "Content Source $($source.Name): last crawl took over 3 hours!"
    }
}
4
  • Couldnt able to parse the final value to do a comparison like if the crawl duration is more than 3 hours send an alert. – Achiever SP Sep 6 '17 at 23:08
  • I've updated my answer to provide you with code on how to achieve this alert. – Ornery Walrus Sep 6 '17 at 23:59
  • $log = New-Object Microsoft.Office.Server.Search.Administration.CrawlLog $ssa -- This line throws an error: – Achiever SP Sep 7 '17 at 1:14
  • Whille this script wont work? $sources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application DEV" ForEach ($source in $sources) { if ($source.CrawlStatus -ne "Idle") { $time1 = ((Get-Date) - $source.CrawlStarted) $time1 = Get-Date "$time1" -Format HH:mm:ss if($time1.Hours -ge 3 ) { Write-Host $source.Name $time1 } } } – Achiever SP Sep 7 '17 at 1:25
0
$sources = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication "Search Service Application DEV"
ForEach ($source in $sources) {
    if ($source.CrawlStatus -ne "Idle") {
        $time1 = ((Get-Date) - $source.CrawlStarted)
        $time1 =  Get-Date "$time1" -Format HH:mm:ss
        if($time1.Hours -ge 3 )
        {
            Write-Host $source.Name $time1
        }
    }
}
1
  • Since this question has been answered, I think this should be taken to a new question. – Ornery Walrus Sep 7 '17 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.