PoshCode Archive  Artifact Content

Artifact cdfcc6db1ebbc980d305b8674e838d6d1b0973400f48eb4d5f8d4607c505df2e:

  • File Run-Query-SharePoint.ps1 — part of check-in [12d6f5be5a] at 2018-06-10 12:56:49 on branch trunk — Runs a FullTextSqlQuery (SharePoint’s Enterprise Search SQL Query) against your local MOSS farm; useful as a quick(!) search query test workbench. (user: Peter size: 1436)

# encoding: ascii
# api: powershell
# title: Run-Query (SharePoint)
# description: Runs a FullTextSqlQuery (SharePoint’s Enterprise Search SQL Query) against your local MOSS farm; useful as a quick(!) search query test workbench.
# version: 0.1
# type: function
# author: Peter
# license: CC0
# function: Run-Query
# x-poshcode-id: 1294
# x-derived-from-id: 1304
# x-archived: 2016-03-19T02:55:55
# x-published: 2010-08-25T14:26:00
#
#
function Run-Query($siteUrl, $queryText)
{
	[reflection.assembly]::loadwithpartialname("microsoft.sharePOint") | out-null
	[reflection.assembly]::loadwithpartialname("microsoft.office.server") | out-null
	[reflection.assembly]::loadwithpartialname("microsoft.office.server.search") | out-null
	$s = [microsoft.sharepoint.spsite]$siteUrl
	$q = new-object microsoft.office.server.search.query.fulltextsqlquery -arg $s
	$q.querytext = $queryText
	$q.RowLimit = 100
	$q.ResultTypes = "RelevantResults"
	$dt = $q.Execute()
	$r = $dt["RelevantResults"]

	$output = @()
	
	while ($r.Read()) {
		$o = new-object PSObject

		0..($r.FieldCount-1) | foreach {
			add-member -inputObject $o -memberType "NoteProperty" -name $r.GetName($_) -value $r[$_].ToString()
		}
		
		
		$output += $o
	}
	
	return $output
}



#Sample usage:
#Run-Query -siteUrl "http://dev/" -queryText "SELECT PreferredName, WorkPhone FROM SCOPE() WHERE PreferredName LIKE '%JIM%'"
#
#