Tuesday 23 December 2014

Create SharePoint 2013 Search Service Application using PowerShell

Although we can create SharePoint 2013 search service application from Central Administration,  I prefer creating it through PowerShell as it gives the ability to get rid of GUIDs in Search service Database names. Also, from SharePoint 2013 Central Administration, Its not possible to modify search topology! So, PowerShell is the ideal way to create/configure Search in SharePoint 2013.

Creating Search Service Application in PowerShell includes these 7 steps:
  1. Create an application pool for search service application 
  2. Start search service instances on the server
  3. Create a search service application 
  4. Create a search service application proxy
  5. Create new search service topology 
  6. Create all six components of the search and assign them to the search topology
  7. Activate the search topology
PowerShell Script to Create Search Service Application for SharePoint 2013:
Here is the PowerShell script to create Search service application in SharePoint 2013.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# Specify the Settings for the Search Service Application
$ServerName = (Get-ChildItem env:computername).value
$IndexLocation = "D:\Search Index"
$SearchServiceApplicationName = "Search Service Application"
$SearchServiceApplicationProxyName = "Search Service Application Proxy"
$SearchDatabaseServer = "G1SP2013"
$SearchServiceApplicationDatabase = "SP2013_Search_Service"
$SearchAppPoolName = "Search Service Application pool"
$SearchAppPoolAccount =  Get-SPManagedAccount "ME\mossfarmadmin"
#*** Step 1: Create Application Pool for Search Service Application ****
#Get the existing Application Pool
$SearchServiceAppPool = Get-SPServiceApplicationPool -Identity $SearchAppPoolName
#If Application pool Doesn't exists, Create it
if (!$SearchServiceAppPool)
{
    $SearchServiceAppPool = New-SPServiceApplicationPool -Name $SearchAppPoolName -Account $SearchAppPoolAccount
    write-host "Created New Application Pool" -ForegroundColor Green
}
#*** Step 2: Start Search Service Instances ***
Start-SPEnterpriseSearchServiceInstance $ServerName -ErrorAction SilentlyContinue
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $ServerName -ErrorAction SilentlyContinue
#*** Step 3: Create Search Service Application ****
# Get the Search Service Application
$SearchServiceApplication = Get-SPEnterpriseSearchServiceApplication -Identity $SearchServiceApplicationName -ErrorAction SilentlyContinue
# Create the Search Service Application, If it doesn't exists!
if(!$SearchServiceApplication)
{
    $SearchServiceApplication = New-SPEnterpriseSearchServiceApplication -Name $SearchServiceApplicationName -ApplicationPool $SearchServiceAppPool -DatabaseServer $SearchDatabaseServer -DatabaseName $SearchServiceApplicationDatabase
    write-host "Created New Search Service Application" -ForegroundColor Green
}
#*** Step 4: Create Search Service Application Proxy ****
 #Get the Search Service Application Proxy
 $SearchServiceAppProxy = Get-SPEnterpriseSearchServiceApplicationProxy -Identity $SearchServiceApplicationProxyName -ErrorAction SilentlyContinue
 # Create the Proxy, If it doesn't exists!
if(!$SearchServiceAppProxy)
{
    $SearchServiceAppProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name $SearchServiceApplicationProxyName -SearchApplication $SearchServiceApplication
    write-host "Created New Search Service Application Proxy" -ForegroundColor Green
}
#*** Step 5: Create New Search Topology
$SearchServiceInstance = Get-SPEnterpriseSearchServiceInstance -Local
#To Get Search Service Instance on Other Servers: use - $SearchServiceAppSrv1 = Get-SPEnterpriseSearchServiceInstance -Identity "<Server Name>"
# Create New Search Topology
$SearchTopology =  New-SPEnterpriseSearchTopology -SearchApplication $SearchServiceApplication
#*** Step 6: Create Components of Search
New-SPEnterpriseSearchContentProcessingComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance
New-SPEnterpriseSearchAnalyticsProcessingComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance
New-SPEnterpriseSearchCrawlComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance
New-SPEnterpriseSearchAdminComponent -SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance
#Prepare Index Location
Remove-Item -Recurse -Force -LiteralPath $IndexLocation -ErrorAction SilentlyContinue
MKDIR -Path $IndexLocation -Force
#Create Index and Query Components
New-SPEnterpriseSearchIndexComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance -RootDirectory $IndexLocation
New-SPEnterpriseSearchQueryProcessingComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance
#*** Step 7: Activate the Toplogy for Search Service ***
$SearchTopology.Activate() # Or Use: Set-SPEnterpriseSearchTopology -Identity $SearchTopology

Here is the Search center topology created:
Create SharePoint 2013 Search Service Application using PowerShell
As you see in the above screen, Provisioning search service application creates 4 databases:  I'm naming them as follows:
  • SP2013_Search_Service - Search Service Administration database stores configuration and topology (It could be better: SP2013_Search_Service_AdminDB")
  • SP2013_Search_Service_AnalyticReporting- Stores the result of usage analysis report.
  • SP2013_Search_Service_CrawlStore - The crawl database contains detailed tracking and historical information about crawled items
  • SP2013_Search_Service_LinksStore - Link database, Stores the information extracted by the content processing component & click-through information of searched items.

Configuring SharePoint 2013 Search Service Application for Multi-Server Farm:

The above code assumes you are creating search for Standalone installation (Single Server Topology). However, You may have to distribute search components/Add Search Components to different WFE/App servers in a Multi-Server Farm. To do so, Just change Search Service Instance as:

$SearchServiceInstance = Get-SPEnterpriseSearchServiceInstance -Identity "Server-Name" 

and change other search components accordingly. Lets take an example: Lets say, We've two SharePoint App Servers "G1SP2013-001" and "G1SP2013-002", wants to distribute Search components among them:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$AppSrv1 ="G1SP2013-001"
$AppSrv2 ="G1SP2013-002"
$SearchServiceInstance1 = Get-SPEnterpriseSearchServiceInstance -Identity $AppSrv1
$SearchServiceInstance2 = Get-SPEnterpriseSearchServiceInstance -Identity $AppSrv2
#Lets say, We need two crawl components
New-SPEnterpriseSearchCrawlComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance1
New-SPEnterpriseSearchCrawlComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance1
# Lets say, We need two Index components
# Set the primary and replica index locations
$PrimaryIndexLocation = "D:\SearchIndex"
$ReplicaIndexLocation = "E:\SearchIndexReplica"
  
#We need two index partitions and replicas for each partition. Follow this sequence.
New-SPEnterpriseSearchIndexComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance1 -RootDirectory $PrimaryIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance2 -RootDirectory $ReplicaIndexLocation -IndexPartition 0
New-SPEnterpriseSearchIndexComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance2 -RootDirectory $PrimaryIndexLocation -IndexPartition 1
New-SPEnterpriseSearchIndexComponent –SearchTopology $SearchTopology -SearchServiceInstance $SearchServiceInstance1 -RootDirectory $ReplicaIndexLocation -IndexPartition 1


Read more: http://www.sharepointdiary.com/2013/11/create-search-service-application-using-powershell.html#ixzz3Mip5iIKw