PowerShell script to search SharePoint Search WebService via SOAP

I wanted to copy all the files found in a SharePoint Search result for a scope that lived on a file share. So I wrote the following PowerShell code to query a SharePoint Search scope and find the url for each result.

#$KeywordQuery="Cows"
$SQLQuery="SELECT WorkId,Path,Title,Write,Author from Scope() WHERE `"Scope`"='NarrowScope' AND FREETEXT(defaultproperties,'Cows')"

$CountToReturn = 1000
$xmlDoc = new-object System.Xml.XmlDocument
$QueryPacket = $xmlDoc.CreateElement("QueryPacket")
$QueryPacket.SetAttribute("xmlns", "urn:Microsoft.Search.Query")
$Query = $xmlDoc.CreateElement("Query")
[void]$querypacket.AppendChild($Query)
$Context = $xmlDoc.CreateElement("Context")
[void]$query.AppendChild($Context)
$QueryText = $xmlDoc.CreateElement("QueryText")

#If you are Using the KeyWord method
#$QueryText.SetAttribute("type", "string") 
#$QueryText.set_InnerXMl($KeywordQuery)

#IF you are using SQL method
$QueryText.SetAttribute("type", "MSSQLFT")
$QueryText.set_InnerXMl($SQLQuery)

[void]$context.AppendChild($QueryText)
$Range = $xmlDoc.CreateElement("Range")
[void]$query.AppendChild($Range)
$Count = $xmlDoc.CreateElement("Count")
$Count.set_InnerXMl($CountToReturn)
[void]$range.AppendChild($Count)

$IncludeSpecialTermResults=$xmlDoc.CreateElement("IncludeSpecialTermResults")
$IncludeSpecialTermResults.set_InnerXML("false")
[void]$query.AppendChild($IncludeSpecialTermResults)

$PreQuerySuggestions=$xmlDoc.CreateElement("PreQuerySuggestions")
$PreQuerySuggestions.set_InnerXML("false")
[void]$query.AppendChild($PreQuerySuggestions)

$HighlightQuerySuggestions=$xmlDoc.CreateElement("HighlightQuerySuggestions")
$HighlightQuerySuggestions.set_InnerXML("false")
[void]$query.AppendChild($HighlightQuerySuggestions)

$IncludeRelevantResults=$xmlDoc.CreateElement("IncludeRelevantResults")
$IncludeRelevantResults.set_InnerXML("false")
[void]$query.AppendChild($IncludeRelevantResults)

$IncludeHighConfidenceResults=$xmlDoc.CreateElement("IncludeHighConfidenceResults")
$IncludeHighConfidenceResults.set_InnerXML("false")
[void]$query.AppendChild($IncludeHighConfidenceResults)

$Service = New-WebServiceProxy -UseDefaultCredential -uri http://sharepoint.search.com/Search/_vti_bin/Search.asmx
[ xml ]$Results = $Service.Query($QueryPacket.OuterXml)
write-host "Results=$($Results.ResponsePacket.Response.Range.Count)"
foreach ($Doc in $Results.ResponsePacket.Response.Range.Results.Document){
$PATH=$($Doc.Action.LinkUrl.("#text")).Replace("/","\").Replace("file:","")
write-host $PATH
#here is where you put your copy cmd
cp $PATH c:\Temp
}

,

Comments are closed.

Powered by WordPress. Designed by WooThemes