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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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