Get Crawl Status Using Powershell

{Edit} I’ve found another way of doing this{/}

Way One:

I needed a way of knowing when a crawl status was set to paused.  Using a combination of Windows Task, Powershell, SharePoint List, and Workflow I was able to come up with a solution.

Process: Windows Task runs every hour (on the server with Central Administration).  This Task runs my Powershell command.  The command checks all my Content Sources for a crawl status of paused. If the status is paused, the command will write an Item to a SharePoint List.  Associated Workflow on the List then sends me an email.

Link to the completed Powershell command: Powershell Crawl Status (you will need to open the file and save it with a .ps1 extension.)

[void] [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

$SITEURL = "http://win-severname"

$spsite = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$serverContext = [Microsoft.Office.Server.ServerContext]::Default
$context = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($serverContext)
$sspcontent = new-object Microsoft.Office.Server.Search.Administration.Content($context)
$sspContentSources = $sspcontent.ContentSources

[int]$count

$count=0

foreach ($cs in $sspContentSources)

{
if ($cs.CrawlStatus -eq [Microsoft.Office.Server.Search.Administration.CrawlStatus]::Paused)
{

$count++

}
}

if ($count -gt 0)

{

$spweb = $spsite.OpenWeb()
$splist = $spweb.Lists["ListName"];
$items=$splist.get_items() | where { $_.Title -like '*' }

if ($splist.ItemCount -gt 0)
{
$items | % { $_.Delete() }
}
$newItem = $splist.Items.Add()
$newItem["Title"] = "NewItem"
$newItem.Update()

$spweb.Dispose()
$spsite.Dispose()


}

——————————

How to run a Powershell command from a Scheduled Task.

To run a Powershell command you will need to do the following.

1.In the Actions tab:

a.Click New. The New Action dialog box appears.

2.In Settings, in Program/Script, type:

powershell.exe

3.In Add arguments, type the following:

-command “C:\Powershell Crawl Status.ps1”

4.Click OK.

http://technet.microsoft.com/en-us/library/ee649304(WS.10).aspx

Way Two and the EASY way:


$web = New-Object System.Net.WebClient
$web.UseDefaultCredentials = $true

If ($web.DownloadString(“http://craigslistSpam/ssp/admin/_layouts/listcontentsources.aspx”)| select-string “Paused” -CaseSensitive)
{
$emailFrom = "me@craigslistSpam.com"
$emailTo = "you@craigslistSpam.com"
$subject = "testing"
$body = "test email"
$smtpServer = "this can be found in Outlook"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}