Create a Search Service SharePoint 2010 with PowerShell

To create and configure a Search Service for SharePoint 2010 with Powershell you can use the following script:

Reviewed this Script 24 september 2010 –> changed some code to create databasesnames without GUID’s…

# 1.Setting up initial variables.
write-host 1.Setting up initial variables.
$Search_Service_name = "aName_Search_app"
$Search_Service_account = "domainaccountName"
$Search_Service_Instance = get-spenterprisesearchserviceinstance -local
$err = $null

# Start Services search services for Search_Service_Instance
write-host Start Services search services for Search_Service_Instance
Start-SPEnterpriseSearchServiceInstance -Identity $Search_Service_Instance

# 2.Create an Application Pool.
write-host 2.Create an Application Pool.
$AppPool = new-SPServiceApplicationPool -name $Search_Service_name"_AppPool" -account $Search_Service_account

# 3.Create the SearchApplicationlication and set it to a variable
write-host 3.Create the SearchApplicationlication and set it to a variable
$SearchApplication = New-SPEnterpriseSearchServiceApplication -Name $Search_Service_name -applicationpool $AppPool -databasename $Search_Service_name"_AdminDB"

#4 Create search service application proxy
write-host 4 Create search service application proxy
$Search_Service_Proxy = new-spenterprisesearchserviceapplicationproxy -name $Search_Service_name"ApplicationProxy" -Uri $SearchApplication.Uri.AbsoluteURI

# 5.Provision Search Admin Component.
write-host 5.Provision Search Admin Component.
set-SPenterprisesearchadministrationcomponent -SearchApplication $SearchApplication  -searchserviceinstance $Search_Service_Instance

# 6.Create a new Crawl Topology.
write-host 6.Create a new Crawl Topology.
$CrawlTopo = $SearchApplication | New-SPEnterpriseSearchCrawlTopology

# 7.Create a new Crawl Store.
write-host 7.Create a new Crawl Store.
$CrawlStore = $SearchApplication | Get-SPEnterpriseSearchCrawlDatabase

# 8.Create a new Crawl Component.
write-host 8.Create a new Crawl Component.
New-SPEnterpriseSearchCrawlComponent -CrawlTopology $CrawlTopo -CrawlDatabase $CrawlStore -SearchServiceInstance $Search_Service_Instance

# 9.Activate the Crawl Topology.
write-host 9.Activate the Crawl Topology.
do
{
    $err = $null
    $CrawlTopo | Set-SPEnterpriseSearchCrawlTopology -Active -ErrorVariable err
    if ($CrawlTopo.State -eq "Active")
    {
        $err = $null
    }
    Start-Sleep -Seconds 10
}
until ($err -eq $null)

# 10.Create a new Query Topology.
write-host 10.Create a new Query Topology.
$QueryTopo = $SearchApplication | New-SPenterpriseSEarchQueryTopology -partitions 1

# 11.Create a variable for the Query Partition
write-host 11.Create a variable for the Query Partition
$Partition1 = ($QueryTopo | Get-SPEnterpriseSearchIndexPartition)

# 12.Create a Query Component.
write-host 12.Create a Query Component.
New-SPEnterpriseSearchQueryComponent -indexpartition $Partition1 -QueryTopology $QueryTopo -SearchServiceInstance $Search_Service_Instance

# 13.Create a variable for the Property Store DB.
write-host 13.Create a variable for the Property Store DB.
$PropDB = $SearchApplication | Get-SPEnterpriseSearchPropertyDatabase

# 14.Set the Query Partition to use the Property Store DB.
write-host 14.Set the Query Partition to use the Property Store DB.
$Partition1 | Set-SPEnterpriseSearchIndexPartition -PropertyDatabase $PropDB

# 15.Activate the Query Topology.
write-host 15.Activate the Query Topology.
do
{
    $err = $null
    $QueryTopo | Set-SPEnterpriseSearchQueryTopology -Active -ErrorVariable err -ErrorAction SilentlyContinue
    Start-Sleep -Seconds 10
    if ($QueryTopo.State -eq "Active")
        {
            $err = $null
        }
}
until ($err -eq $null)

Write-host "Your search application $Search_Service_name is now ready"

Tested this script with good results and no errors in the Crawl logs:

6 thoughts on “Create a Search Service SharePoint 2010 with PowerShell

  1. Thank you, I have been suffering with the GUID in the database names, and your article saved me, I removed the existing Search Service App and then used your code, worked like a charm!
    Million thanks!

  2. How long should lines 75-86 take to run? Mine seems to be hanging there. It has been running for over an hour. Thanks!

  3. Thanks for the quick reply. I modified your script a little so it would tell me what error is happening and it is saying:

    Set-SPEnterpriseSearchQueryTopology : Topology activation was aborted because of System.IO.IOException: I/O error occurred.
    at Microsoft.SharePoint.Administration.SPServer.CreateFileShare(String name, String description, String path)
    at Microsoft.Office.Server.Search.Administration.QueryComponent.CreatePropagationShare(QueryComponent component)
    at Microsoft.Office.Server.Search.Administration.QueryComponent.ExecuteCurrentStage(). To retry the activation, run the cmdlet Set-SPEnterpriseSearchQueryTopology -Acti
    ve. If the error persists, restart the activation by calling the CancelActivate() method on the query topology object, and then calling Set-SPEnterpriseSearchQueryTopology
    -Active.
    At C:UsersAdministratorDesktopReBuildSearch_Marcus.ps1:45 char:53
    + $QueryTopology | Set-SPEnterpriseSearchQueryTopology <<<< -Active
    + CategoryInfo : InvalidData: (Microsoft.Offic…chQueryTopology:SetSearchQueryTopology) [Set-SPEnterpriseSearchQueryTopology], QueryTopologyActivationExcept
    ion
    + FullyQualifiedErrorId : Microsoft.Office.Server.Search.Cmdlet.SetSearchQueryTopology

  4. Did you use (to run the ps script) an account with enough permissions on the server?
    I looks like the share on the server couldn’t be created by the script. The search app creates a share in th hyves folder where it stores search data.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>