Useful Powershell cmdlets for SharePoint Search

You can basically do everything you want for Sharepoint Search configuration in powershell script. This information is spreaded all over Internet, so I give you here a little summary of the different cmdlets you’d like to use in your deployment scripts with small examples. Refer to the msdn documentation, if you need more informations on the cmdlets.

Remove content sources

$SearchServiceApplication = "Search Service Application"
$Name = "Company Products"
$contentSource = Get-SPEnterpriseSearchCrawlContentSource $Name -SearchApplication $SearchApplication -ErrorAction SilentlyContinue
if ($contentSource -ne $null)
{
   $contentSource | Remove-SPEnterpriseSearchCrawlContentSource
}

Create content sources

# SharePoint type
$Name = "Company site"
$StartAddress = "http://www.companysite.com"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApplication -Type Sharepoint -name $Name -StartAddresses $StartAddress

# Business type
$Name = "Company Products"
$lobSystems = @("ProductConnector","ProductConnector")
$proxyGroup = Get-SPServiceApplicationProxyGroup -default
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApplication -Type Business -name $Name -LOBSystemSet $lobSystems -BDCApplicationProxyGroup $proxyGroup

# Web site
$Name = "Other Company Site"
$StartAddress = "http://www.companysitenotinsp.com"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApplication -Type Web -name $Name -StartAddresses $StartAddress -MaxSiteEnumerationDepth 0

# Shared folder
$Name = "Shared things"
$StartAddress = "\\sharedFolder\folder with things in it"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $SearchApplication -Type File -name $Name -StartAddresses $StartAddress

Start Crawl

$contentSource = Get-SPEnterpriseSearchCrawlContentSource $Name -SearchApplication $SearchServiceApplication
$contentSource.StartFullCrawl()  #Or StartIncrementalCrawl for an incremental

Create crawled properties

$searchapp = Get-SPEnterpriseSearchServiceApplication $SearchServiceApplication
$categoryName = "Business Data"
$PropSetGuid = "2edeba9a-0fa8-4020-8a8b-30c3cdf34ccd" # Always the same for type Business Data
$category = Get-SPEnterpriseSearchMetadataCategory -SearchApplication $searchapp -Identity $categoryName
$crawledPropName = "CompanyProduct.Description"
$category.CreateCrawledProperty($crawledPropName, $false, $PropSetGuid)

Create managed properties
For more information about the Type number in this cmdlet, see this msdn reference.

$managedPropName = "Description"
$dataType = 1 # For text... See reference above
New-SPEnterpriseSearchMetadataManagedProperty -Name $managedPropName -SearchApplication $SearchServiceApplication -Type $dataType -Queryable $true -Retrievable $true -SafeForAnonymous $true -EnabledForScoping $true

Delete crawled properties

# You cannot delete specific crawled property, so unmap them, and remove the unmapped properties
foreach($property in $category.GetAllCrawledProperties())
{
  Set-SPEnterpriseSearchMetadataCrawledProperty -Identity $property -IsMappedToContents $false
}
$category.DeleteUnmappedProperties()
$searchapp.Update()

Delete managed properties

Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $SearchServiceApplication -Identity $managedPropName | Remove-SPEnterpriseSearchMetadataManagedProperty

Create a custom ranking profile

$owner = Get-SPEnterpriseSearchOwner -Level Ssa
$rankmodel = [string] Get-Content "C:\RankProfileModel.xml" # Look at the existing ranking model to give you inspiration, use Get-SPEnterpriseSearchRankingModel
New-SPEnterpriseSearchRankingModel –SearchApplication $SearchServiceApplication –Owner $owner -RankingModelXML $rankmodel

Create Content Enrichment Web Service

# Create the appPool and webService
$SearchWebServiceAppPoolName = "ContentEnrichmentWebService"
$SearchWebServiceName = "ContentEnrichmentWebService"
$SearchWebServicePort = 7777 # The port number is at your discretion
$appPool = New-WebAppPool $SearchWebServiceAppPoolName
$appPool.processModel.username = [string]("DOMAIN\User")
$appPool.processModel.password = [string]("Password1")
$appPool.processModel.identityType = 3
$appPool | set-item
$appPool | set-itemproperty -Name "managedRuntimeVersion" -Value "v4.0" -Force:$true
New-WebSite -Name $SearchWebServiceName -Port $SearchWebServicePort -PhysicalPath "C:\ReposOfTheWebService" -ApplicationPool $SearchWebServiceAppPoolName

# Set the content Enrichment on the webService
$ssa = Get-SPEnterpriseSearchServiceApplication
$config = New-SPEnterpriseSearchContentEnrichmentConfiguration
$config.DebugMode = $false
$config.Endpoint = "http://localhost:$SearchWebServicePort/$SearchWebServiceName"
$config.FailureMode = "WARNING"
$config.inputProperties = {"Name","Description"} # You can put as many Managed Prop that you want here, make sure they exist before trying to add them here.
$config.outputProperties = {"TranformedName","TranformedDescription"} # You can put as many Managed Prop that you want here, make sure they exist before trying to add them here.
$config.SendRawData = $false
Set-SPEnterpriseSearchContentEnrichmentConfiguration –SearchApplication $ssa –ContentEnrichmentConfiguration $config

Add execute permission on BDC Service

$centralAdminUrl = "http://localhost:18015"
$SearchBDCProductConnectorName = "CompanyProduct"
$SearchBDCProductConnectorNamespace = "Company.Search.BDC.CompanyProduct"
[String[]]$executePermissions=@("Execute")
[String[]]$fullControlPermissions=@("Execute","Edit","SelectableInClients","SetPermissions")
$ECT = Get-SPBusinessDataCatalogMetadataObject -BdcObjectType "Entity" -ServiceContext "$centralAdminUrl" -Name "$SearchBDCProductConnectorName" -Namespace "$SearchBDCProductConnectorNamespace" | Select-Object -Last 1

$user = New-SPClaimsPrincipal -Identity "DOMAIN\User" -IdentityType WindowsSamAccountName
$admin = New-SPClaimsPrincipal -Identity "DOMAIN\Admin" -IdentityType WindowsSamAccountName
Grant-SPBusinessDataCatalogMetadataObject -Identity $ECT -Principal $user -Right $executePermissions
Grant-SPBusinessDataCatalogMetadataObject -Identity $ECT -Principal $user -Right $fullControlPermissions

Copy-SPBusinessDataCatalogAclToChildren -MetadataObject $ECT