Provision Search for SharePoint Foundation 2013 using PowerShell with clean DB names

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:

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)

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

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:

So now that we have both our functions identified from reflection, all we have to do is write the PowerShell script.

Here it is:

At this point the search service application will be provisioned without any topology components.

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:

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.

Jasjit Chopra is a MS Cloud Architect with strong background in SharePoint, Office 365 and Azure. As a SharePoint expert he has worked with many multinational clients including HP, Avanade, Accenture, Unistar Nuclear Energy, Warner Music Group, Inventiv Health and iHeartMedia. Jasjit holds a Masters of Business Administration in Technology and Management from CERAM, Sophia Antipolis, France. Having worked for different clients across the United States Jasjit has gained insight knowledge on Business Processes for the State Government and Manufacturing, Nuclear, Pharmaceutical and Music industries.

44 thoughts on “Provision Search for SharePoint Foundation 2013 using PowerShell with clean DB names

  1. Hi Jasjit Chopra
    must i run the second script to?

    “#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)”

    regards
    Tommy

  2. I’m getting an error on this line.

    $methodInfo = $searchServiceApp.GetType().GetMethod(“InitDefaultTopology”, $bindings, $null, $types, $null)

    the error is something like i can’t call the method with $null value.

    ideas? 🙁

    1. Hello Christian,

      I think you called the 2nd Part in a separate ps1 file?
      Then you have no value for ” $searchServiceApp”, which is defined in Part 1 of the script.
      I assume you have to paste poth parts into one single script and execute it, so that the value for ” $searchServiceApp” is filled.

      Am I right, Jasjit?

      Kind Regrads

  3. Hi, We have a SharePoint Foundation 2013 with SP1 Environment.

    In that, we have 2 Front-End Servers and 2 App Servers. In the Front-End Servers, the Search Service is stopped and is in Disabled state and in the 2 App Servers in One App Server, Search is Online and in another Search is Starting but goes to Stopped soon after.

    Originally, we had only 1 App Server and we were running our Search Service and Search Service Application in that. Now since the index location became full and we were unable to increase the drive there, we added one more App Server and now the issue is Search is not properly getting configured in either of these App servers. What we want to do is run Search only in the new App Server, because we have a lot of storage space for Index locations here, but in the older App Server, not run Search at all. We tried keeping the Search Service disabled and ran the below PowerShell Scripts, but none of the ones are working. These scripts are creating the Search Service Application, but the error of “Admin Component is not Online”, “Could not connect to the machine hosting SharePoint 2013 admin component” is coming up.

    I did follow steps in this blog, but of no positive results. Would greatly appreciate if I can get some guidance on getting this fixed please.

  4. Hello Jasjit,

    This script was the only solution, that helped me to get rid of “Getting results failed: Microsoft.Office.Server.Search.Query.InternalQueryErrorException” error, whilst trying to use Enterprise Search on SharePoint Foundation 2013 in Dynamics AX 2012 R2.

    Thank you!

  5. Hi…

    Same issue as Cristian… here is the error…

    PS C:\Temp> $searchServiceInstance = Get-SPEnterpriseSearchServiceInstance -Local
    PS C:\Temp> $bindings = @(“InvokeMethod”, “NonPublic”, “Instance”)
    PS C:\Temp> $types = @([Microsoft.Office.Server.Search.Administration.SearchServiceInstance])
    PS C:\Temp> $values = @([Microsoft.Office.Server.Search.Administration.SearchServiceInstance]$searchServiceInstance)
    PS C:\Temp> $methodInfo = $searchServiceApp.GetType().GetMethod(“InitDefaultTopology”, $bindings, $null, $types, $null)

    You cannot call a method on a null-valued expression.
    At line:1 char:1
    + $methodInfo = $searchServiceApp.GetType().GetMethod(“InitDefaultTopology”, $bind …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Thanks for your help

    Francis

    1. Simply run all the script in one session or one powershell script file. Running the second part on its own will have this error. It is relying on variables form the above section.

  6. i have execute just the script, now i have the problem that i could excute the script with additional topology settings… please could someone help me?

  7. when i run the following command

    $searchServiceApp = $searchService.CreateApplication($serviceAppName, $adminDBParameters, $propertyDBParameters, $crawlStoreDBParameters, $analyticsStoreDBParameters, $linkStoreDBParameters, [Microsoft.SharePoint.Administration.SPIisWebServiceApplicationPool]$svcPool, [Microsoft.SharePoint.Administration.SPIisWebServiceApplicationPool]$adminPool)

    i get the following error

    Exception calling “CreateApplication” with “8” argument(s): “Value cannot be
    null.
    Parameter name: databaseParameters”
    At line:1 char:1
    + $searchServiceApp = $searchService.CreateApplication($serviceAppName,
    $adminDBPa …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

    can you give any suggestions?

    1. Actually now I can see using Get-SPEnterpriseSearchServiceApplication that topology created but UI still shows the error you have in the screenshot

  8. Hello Jasjit Chopra,

    Thanks a million for your great script. I tried it out and it worked perfectly 🙂 I have 2 WFEs, 2 Apps and 1 SQL in the farm. Now search is running on App1 server, I’d like to add search on App2 server too (App2 already joined into the existing farm) for HA (if each App server is down, search still works)

    So please show me how to customise the script to do that

    Looking forward to hearing from you soon
    Many thanks,

  9. Hello Jasjit Chopa,

    Many thanks for your reply. So you mean that foundation doesn’t support scalability for search only ? Or everything ? (E.g. How about load balancing for 2 wfes)

    Thanks,
    Khoa

  10. Hey! Awesome work! Is there also an Option to add a index location? i know that SP Standard got this but im not very familiar with foundation :S

Leave a Reply