For those who don’t know, SharePoint Foundation 2013 only allows you to create Search Service Application with the built-in Wizard from the Central Admin GUI. The drawback is the trailing guid’s at the end of the database names that end up in your SQL server instance.
And this is what you get when you try to use PowerShell to create the Search Service Application in SharePoint Foundation 2013:
Image may be NSFW.
Clik here to view.
NOTE: If you don’t understand this well, please do not attempt to run this script. Microsoft will probably not support your farm or search application built this way.
A very BIG Thank you note to Gary Lapointe for his post here. I took his script a bit further and tweaked it to use custom DB names and further create the default topology using reflection in PowerShell.
We are going to take a deeper look with .Net Reflector behind the scenes to identify which functions we need to call and what parameters they use.
Browse the Microsoft.Office.Server.Search.dll via reflector (In case you are wondering you can check the location of this dll in the below screenshot)
Image may be NSFW.
Clik here to view.
Now we are going to navigate to the node where the functions are stored for creating Search Service Application. For this we need to traverse to Microsoft.Office.Server.Search.Administration - SearchService
Image may be NSFW.
Clik here to view.
As you can see in the above screenshot, there are several functions named CreateApplication. The one which we are interested in is this one:
public
SearchServiceApplication
CreateApplication(string name, SPDatabaseParameters adminDBParameters, SPDatabaseParameters propertyStoreDBParameters, SPDatabaseParameters crawlStoreDBParameters, SPDatabaseParameters analyticsReportingStoreDBParameters, SPDatabaseParameters linksStoreDBParameters, SPIisWebServiceApplicationPool applicationPool, SPIisWebServiceApplicationPool adminApplicationPool)
This function will allow us to provide our own DB name parameters.
However, this function will create the Service Application only. We still need a way to create the default topology. So in order to not having go through the entire process of creating each topology components manually via reflection, I took a deep dive into the function which SPF wizard calls internally i.e. CreateApplicationWithDefaultTopology(). Looking into this function I found the internal function it uses to create the default topology all by itself. Let us take a look in reflection:
Image may be NSFW.
Clik here to view.
So now that we have both our functions identified from reflection, all we have to do is write the PowerShell script.
Here it is:
#Start the search service instance on the server Start-SPEnterpriseSearchServiceInstance $env:computername Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $env:computername #Give a unique name to your search service application $serviceAppName = "MyCleanDB Search Service Application" #Get the application pools to use (make sure you change the value for your environment) $svcPool = Get-SPServiceApplicationPool "SharePoint Web Services Default" $adminPool = Get-SPServiceApplicationPool "SharePoint Web Services Default" #Get the service from the service instance so we can call a method on it $searchServiceInstance = Get-SPEnterpriseSearchServiceInstance –Local $searchService = $searchServiceInstance.Service #Define your unique DB names without the guids $adminDB = "Search_Service_Application_DB" $propertyStoreDB = "Search_Service_Application_PropertyStoreDB" $crawlStoreDB = "Search_Service_Application_CrawlStoreDB" $analysticsStoreDB = "Search_Service_Application_AnalyticsStoreDB" $linkStoreDB = "Search_Service_Application_LinkStoreDB" #Since this method takes in the value of object type Microsoft.SharePoint.Administration.SPDatabaseParameters we will create these from our clean DB names $adminDBParameters = [Microsoft.SharePoint.Administration.SPDatabaseParameters]::CreateParameters($adminDB,"None") $propertyDBParameters = [Microsoft.SharePoint.Administration.SPDatabaseParameters]::CreateParameters($propertyStoreDB,"None") $crawlStoreDBParameters = [Microsoft.SharePoint.Administration.SPDatabaseParameters]::CreateParameters($crawlStoreDB,"None") $analyticsStoreDBParameters = [Microsoft.SharePoint.Administration.SPDatabaseParameters]::CreateParameters($analysticsStoreDB,"None") $linkStoreDBParameters = [Microsoft.SharePoint.Administration.SPDatabaseParameters]::CreateParameters($linkStoreDB,"None") #Create the search service application by calling the function $searchServiceApp = $searchService.CreateApplication($serviceAppName, $adminDBParameters, $propertyDBParameters, $crawlStoreDBParameters, $analyticsStoreDBParameters, $linkStoreDBParameters, [Microsoft.SharePoint.Administration.SPIisWebServiceApplicationPool]$svcPool, [Microsoft.SharePoint.Administration.SPIisWebServiceApplicationPool]$adminPool) #Create the search service application proxy as usual (luckily PowerShell for this works and is bot blocked) $searchProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name "$serviceAppName Proxy" -SearchApplication $searchServiceApp #Provision the search service application $searchServiceApp.Provision()
At this point the search service application will be provisioned without any topology components.
Image may be NSFW.
Clik here to view.
#Get an updated handle on the SearchServiceInstance $searchServiceInstance = Get-SPEnterpriseSearchServiceInstance –Local #Now we will call the method to initiate the default topology component creation using reflection $bindings = @("InvokeMethod", "NonPublic", "Instance") $types = @([Microsoft.Office.Server.Search.Administration.SearchServiceInstance]) $values = @([Microsoft.Office.Server.Search.Administration.SearchServiceInstance]$searchServiceInstance) $methodInfo = $searchServiceApp.GetType().GetMethod("InitDefaultTopology", $bindings, $null, $types, $null) $searchTopology = $methodInfo.Invoke($searchServiceApp, $values)
Now you need some patience while all the components spring to life behind the scenes. This can take some time depending how robust your environment is. After a while you will have your search provisioned with clean DBs as shown below:
Image may be NSFW.
Clik here to view.
Happy SharePointing !!
PS: I will be speaking at the biggest SharePoint online conference www.SP24Conf.com on multitenancy where I will show how to create this search application in partitioned mode for use in multitenant environment.