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:


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!
Thanks!
How long should lines 75-86 take to run? Mine seems to be hanging there. It has been running for over an hour. Thanks!
Max 30 minutes.
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:\Users\Administrator\Desktop\ReBuildSearch_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
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.
When i am using this search service application is created successfully but when i click on the Default content source it says following error
I am not able to edit the Default content source
Error
Specified cast is not valid.
Troubleshoot issues with Microsoft SharePoint Foundation.
Correlation ID: f6f2cb3d-f0ed-44e8-bb12-44926d419dbe
Date and Time: 5/3/2012 11:42:51 AM
Go back to site
Hi Ronald,
When i am using the above script, search service application is created successfully but when i click on the Default content source it says following error, Appreciate your quick response.
I am not able to edit the Default content source
Error
Specified cast is not valid.
Troubleshoot issues with Microsoft SharePoint Foundation.
Correlation ID: f6f2cb3d-f0ed-44e8-bb12-44926d419dbe
Date and Time: 5/3/2012 11:42:51 AM
Go back to site
Hi Suresh,
Search for the GUID: f6f2cb3d-f0ed-44e8-bb12-44926d419dbe in the sharepoint logging and post this error. I will try to help you.
ronald
Hi Ronald:
Thanks for your reply.
Error:
Specified cast is not valid.
Troubleshoot issues with Microsoft SharePoint Foundation.
Correlation ID: 5a4a4eb5-8392-42fb-b64c-cdcba949f861
Date and Time: 5/4/2012 11:09:22 AM
ULS Logs for GUID : 5a4a4eb5-8392-42fb-b64c-cdcba949f861
“o query components were found for this search ” it seems the search application wasn’t deployed fully. You need a query component, try to add one in the search application topology or start all over. Remove the search application and create a new one.
it’s good script but
we must put the variable “$err” not “err” when activating crowl and query component
Can you guide us how to avoid creation of multiple search service creation using powershell in sharepoint 2010
if you use this script it should only create one search service application.