How to: Programmatically Export the Crawl History to a CSV File in PowerShell


Hi,


When I came across the article at MSDN How to: Programmatically Export the Crawl History to a CSV File I thought I would never create such a tool just for that specific feature, as you end up with additional requirements in order to create an admin tool.


But today I needed to get data from crawl history, and I didn’t want to get them from SQL (remember it is not supported 😉), so I started to write down a simple powershell script to do it. And then I realized that for this atomic actions, indeed it is a great options!: you give admin people multiple commands that they can use/combine to monitor/get information about the environment (and yes many, many things more)



## SharePoint Reference
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Administration”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search.Administration”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server.Search”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.Office.Server”)


function global:Get-CrawlHistory($url)
{
 trap [Exception] {
  write-error $(“ERROR: ” + $_.Exception.GetType().FullName);
  write-error $(“ERROR: ” + $_.Exception.Message);
 
  continue;   
 }


 $s = new-Object Microsoft.SharePoint.SPSite($url);
 $c = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($s);
 $h = new-Object Microsoft.Office.Server.Search.Administration.CrawlHistory($c);


 Write-OutPut $h.GetCrawlHistory();


 $s.Dispose();
}


Then you can just execute: Get-CrawlHistory -url http://your_site_url/ | Export-Csv your_path_and_file_name


Then you can import to excel and make some charts.


In order to filter the information some useful columns should be denormalized: CrawlType, ContentSourceID, Status.


Cheers!

Comments (9)

  1. Hi, If you have read my previous post you may think, why did you stop there? Well, that is what I thought

  2. Hi, When I was working to get powershell scripts from MSDN samples to get/set information, I created

  3. Pavel Dzemyantsau says:

    what are minimal MOSS rights required for executing this code?

  4. hyze says:

    Hi There,

    Thanks for the sample. FYI, I tried but the CSV file is 0KB. Not sure what I miss or the requiremnt not met. Can you advice?

  5. Brent says:

    This code only works for Sharepoint 2007. Has anyone been able to get crawl logs for Sharepoint 2010?

  6. Daniel says:

    Yes it also works for SharePoint 2010, Brent

  7. Surya says:

    Has anyone been able to get crawl logs for SharePoint 2013 ?