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: