PoshCode Logo PowerShell Code Repository

Run-Query (SharePoint) (modification of post by Peter view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1304"></script>download | new post

Runs a FullTextSqlQuery (SharePoint’s Enterprise Search SQL Query) against your local MOSS farm; useful as a quick(!) search query test workbench.

  1. function Run-Query($siteUrl, $queryText)
  2. {
  3.         [reflection.assembly]::loadwithpartialname("microsoft.sharePOint") | out-null
  4.         [reflection.assembly]::loadwithpartialname("microsoft.office.server") | out-null
  5.         [reflection.assembly]::loadwithpartialname("microsoft.office.server.search") | out-null
  6.         $s = [microsoft.sharepoint.spsite]$siteUrl
  7.         $q = new-object microsoft.office.server.search.query.fulltextsqlquery -arg $s
  8.         $q.querytext = $queryText
  9.         $q.RowLimit = 100
  10.         $q.ResultTypes = "RelevantResults"
  11.         $dt = $q.Execute()
  12.         $r = $dt["RelevantResults"]
  13.  
  14.         $output = @()
  15.        
  16.         while ($r.Read()) {
  17.                 $o = new-object PSObject
  18.  
  19.                 0..($r.FieldCount-1) | foreach {
  20.                         add-member -inputObject $o -memberType "NoteProperty" -name $r.GetName($_) -value $r[$_].ToString()
  21.                 }
  22.                
  23.                
  24.                 $output += $o
  25.         }
  26.        
  27.         return $output
  28. }
  29.  
  30.  
  31.  
  32.  
  33. #Sample usage:
  34. #Run-Query -siteUrl "http://dev/" -queryText "SELECT PreferredName, WorkPhone FROM SCOPE() WHERE ""scope""='People' AND PreferredName LIKE '%JIM%'"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me