Export SharePoint 2007 Search Crawl Log Errors using PowerShell

Requirement: I found many errors logged in the Search crawl log, and I wanted to analyse them. So need them in CSV format.

Solution: Let’s use this PowerShell Script to export search crawl errors to CSV:

 #Load SharePoint Assemblies
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")
 
 #Web Application URL
 $url = "https://sharepoint.crescent.com"
 $csID = 4  #Content source id - Local SharePoint sites
      
 #Objects to get Crawl History
 $site = new-Object Microsoft.SharePoint.SPSite($url);
 $context = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site);
 $logViewer = new-object Microsoft.Office.Server.Search.Administration.logviewer ($context)
   
 $crawlLogFilters = New-Object Microsoft.Office.Server.Search.Administration.CrawlLogFilters
 
 $EndDate = [System.DateTime]::Now
 $StartDate=$endDate.AddDays(-1)
 
 #Content Source Id
 $csProp=[Microsoft.Office.Server.Search.Administration.CrawlLogFilterProperty]::ContentSourceId 
 $crawlLogFilters.AddFilter($csProp, $csID)
 #Get all Errors
 $crawlLogFilters.AddFilter([Microsoft.Office.Server.Search.Administration.MessageType]::Error)  
 #Add Start Date and End Date
 $crawlLogFilters.AddFilter($startDate, $endDate)
 
$startNum = 0
$errorItems += $logViewer.GetCurrentCrawlLogData($crawlLogFilters, ([ref] $startNum))

while($startNum -ne -1)
{
  $crawlLogFilters.AddFilter("StartAt", $startNum)
  $startNum = 0; 
  $errorItems += $logViewer.GetCurrentCrawlLogData($crawlLogFilters, ([ref] $startNum))
  #Write-Host "Processing $startNum";
}

$errorItems | Export-CSV "d:\Backup\crawllog.csv" -notypeinformation

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *