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!"
}
}