Skip to content

Instantly share code, notes, and snippets.

@sqlsolver
Last active March 15, 2016 16:31
Show Gist options
  • Save sqlsolver/8f0f1d7c166be75dcfd7 to your computer and use it in GitHub Desktop.
Save sqlsolver/8f0f1d7c166be75dcfd7 to your computer and use it in GitHub Desktop.
Create SharePoint 2013 Search Service Application
<#
.SYNOPSIS
Create a new Search Service Application in a SharePoint 2013 farm.
.DESCRIPTION
Creates a new search service in a farm using service application, proxy and database names customized to the environment via user input. The script also creates
a new app pool or allows use of an existing one. The script is suitable for a small search farm topology and separates components among two servers. It also allows
you to specify a path for index files. Prior to running this script you must provision a search service account and assign it correct permissions.
.EXAMPLE
From a shell admin's PowerShell prompt run .\createNewSSA.ps1 The script is interactive you will need to supply values at the console including credentials for the search and app pool accounts,
the an acronym (no spaces or special characters, caps recommended) for the enivornment, and the server names on which you wish to run components.
.NOTES
15-07-30 Assembled by Ramona Maxwell www.SolverInc.com/contact - Microsoft Public License (Ms-PL) USE AT YOUR OWN RISK, you assume all
liability for your use of any code demonstrated in this gist. This script borrowed its original structure from Todd Klindt&apos;s post
at http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=378 who in turn thanked Spence Harbar for his version.
.LINK
http://www.microsoft.com/en-us/openness/licenses.aspx
#>
#Search service name and variable names concatenate user input with functional description
$prefixName = Read-Host "Please enter an acronym representing your environment without spaces"
$newSSAAppPoolName = $prefixName + " Search Service Application Pool"
#App pool account
$credAppPool = Get-Credential -Message "Please enter the Application Pool service account credentials"
# Create a new app pool for the search service
function newAppPool () {
$newSearchApplicationPool = New-SPServiceApplicationPool -Name $newSSAAppPoolName -Account $credAppPool.username
$script:saAppPool = Get-SPServiceApplicationPool $newSearchApplicationPool
Write-Host "The application pool " $newSearchApplicationPool.Name " has been successfully created."
}
# Get existing app pool in case you are reinstalling
function oldAppPool () {
$getExistingAppPoolName = Read-Host "Please enter the name of the existing app pool you wish to use"
$script:saAppPool = Get-SPServiceApplicationPool $getExistingAppPoolName
if ($saAppPool -ne $null) {
Write-Host "Your search application will use the $saAppPool application pool."
}
else {
Write-Host "The application pool you specified was not found. You must create a new application pool."
newAppPool
}
}
#Select whether to use an existing app pool or create a new one
function selectAppPool () {
Do {
Write-Host "
----------Select App Pool----------
1 = Creat a new Service Application Pool
2 = Use an existing Service Application Pool
-----------------------------------"
$choice1 = Read-Host -Prompt "Select number and press enter"
} until ($choice1 -eq "1" -or $choice1 -eq "2")
Switch ($choice1) {
"1" {newAppPool}
"2" {oldAppPool}
}
}
selectAppPool
# Search application and database names
$serviceAppName = $prefixName + " Enterprise Search Application"
$serviceAppProxyName = $serviceAppName + " Proxy"
$searchDBName = $prefixName + "_EnterpriseSearchApplication_DB"
# Specify the servers hosting the search service and its components
$app0 = Read-Host "Please enter the name of the server on which you wish to host the admin, crawl, content processing and analytics components on"
$app1 = Read-Host "Please enter the name of the server on which you wish to host the index and query components on"
# Index path
$indexPath = Read-Host "Index file storage location: please enter the UNC path to an empty location on a secondary or lower drive with 500 GB or greater capacity to store index files"
$indexPath2 = Read-Host "Index replica file storage location: please enter the UNC path to an empty location on a secondary or lower drive with 500 GB or greater capacity to store index files"
# Start Search Service Instances
Write-Host "Starting Search Service Instances..."
Start-SPEnterpriseSearchServiceInstance $app0
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $app0
Start-SPEnterpriseSearchServiceInstance $app1
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $app1
#Set Index Location
$app0, $app1 | % {
$svcInstances = (Get-SPServer -Identity $_).serviceinstances | ? { $_.GetType().FullName -eq "Microsoft.Office.Server.Search.Administration.SearchServiceInstance" }
$svcInstances.DefaultIndexLocation = $indexPath
$svcInstances.Update()
$svcInstances.Provision()
}
# Test the app pool
$searchAppPool = Get-SPServiceApplicationPool $saAppPool
Write-Host "The application pool ID and name are: "
Write-Host $searchAppPool.Id `n
Write-Host $searchAppPool.Name
# Create the Search Service Application and Proxy
If ($saAppPool.Id -ne $null) {
Write-Host "Creating Search Service Application and Proxy..."
$searchServiceApp = New-SPEnterpriseSearchServiceApplication -Name $serviceAppName -ApplicationPool $searchAppPool -DatabaseName $searchDBName
$searchProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name $serviceAppProxyName -SearchApplication $searchServiceApp
}
Start-Sleep 3
If ($searchServiceApp.status -eq 'Online') {
Write-Host "The new search service application has been created: " `n
Write-Host $searchServiceApp
}
Else {
Start-Sleep 3
Write-Host "The search application may not be provisioned. Attempting to create intitial topology."
}
#test topology
$currentSearchTopology = Get-SPEnterpriseSearchTopology -SearchApplication $searchServiceApp -Active
Write-Host "The current topology is: "
Write-Host $currentSearchTopology
# Clone the default Topology (which is empty) and create a new one and then activate it
Write-Host "Configuring Search Component Topology..."
$clone = $searchServiceApp.ActiveTopology.Clone()
#Test the topology is in scope
If ($clone -ne $null) {
Write-Host "The current topology contains: "
Write-Host $clone
}
Else {
Write-Host "The script was not able to load the existing topology. Exiting to prompt."
Break
}
$SSI_0 = Get-SPEnterpriseSearchServiceInstance $app0
$SSI_1 = Get-SPEnterpriseSearchServiceInstance $app1
New-SPEnterpriseSearchAdminComponent –SearchTopology $clone -SearchServiceInstance $SSI_0
New-SPEnterpriseSearchContentProcessingComponent –SearchTopology $clone -SearchServiceInstance $SSI_0
New-SPEnterpriseSearchAnalyticsProcessingComponent –SearchTopology $clone -SearchServiceInstance $SSI_0
New-SPEnterpriseSearchCrawlComponent –SearchTopology $clone -SearchServiceInstance $SSI_0
New-SPEnterpriseSearchQueryProcessingComponent –SearchTopology $clone -SearchServiceInstance $SSI_1
# Create an index component for the first partition and replicate it on the other server
New-SPEnterpriseSearchIndexComponent –SearchTopology $clone -SearchServiceInstance $app1 -RootDirectory $indexPath -IndexPartition 0
New-SPEnterpriseSearchIndexComponent –SearchTopology $clone -SearchServiceInstance $app0 -RootDirectory $indexPath2 -IndexPartition 0
$clone.Activate()
# Setup the default Content Access Account
Write-Host "Setting the default content access account..."
$credSearchAcct = Get-Credential -Message "Please enter the content access account credentials for the Search Service Application"
try {
Set-SPEnterpriseSearchServiceApplication –Identity $searchServiceApp -DefaultContentAccessAccountName $credSearchAcct.username -DefaultContentAccessAccountPassword $credSearchAcct.password
}
catch {
Write-Host "Setting the default content access account was not successful. Please set the account in the Search Administration console."
Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
Write-Host "Please verify the default content access account in the search configuration following installation."
}
Write-Host "The search service application $serviceAppName has been installed. Exiting to prompt."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment