PowerShell: SharePoint - Create Search Crawl Rule for PHP Site

Updated on 7/12/2013
This script fixes an issue with crawl rules for PHP sites with form based auth. The issue is that the php login page is outputting PHPSESSID as a hidden input field. When entering credentials on the Edit crawl rule page, this PHPSESSID value is getting stored. Storing and passing this value each time the crawler authenticates is causing the failure. This value should change each time a user logs in. Authentication is failing because the crawler is passing an expired PHPSESSID.

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search")
[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Search.Administration")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration")

$site = Get-SPSite http://sharepoint
$context = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($site)
$content = new-object Microsoft.Office.Server.Search.Administration.Content($context)
$rule = $content.CrawlRules.Create([Microsoft.Office.Server.Search.Administration.CrawlRuleType]::InclusionRule, "http://phpsitehost/*")

$privateInputs = New-Object System.Collections.Specialized.NameValueCollection
$publicInputs = New-Object System.Collections.Specialized.NameValueCollection
$errorPages = New-Object System.Collections.Specialized.StringCollection


$privateInputs.Add("password", "")
$privateInputs.Add("email", "")
$publicInputs.Add("from", "")
$errorPages.Add("http://phpsitehost/login.php")

$rule.SetCredentials([Microsoft.Office.Server.Search.Administration.CrawlRuleAuthenticationType]::FormRuleAccess, "POST", "http://phpsitehost/login.php", $publicInputs, $privateInputs, $errorPages)
$rule.FollowComplexUrls = $true
$rule.Update()
echo "Done"