<#====================================================================
Copyright © 2015, September. Michael Pomfret
Creates the Search Service Application
Enables Enterprise Search. Needs Usage Service Application to be created first or creates one itself if missing.
====================================================================#>
#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window
If ((Get-PsSnapin |?{$_.Name -eq “Microsoft.SharePoint.PowerShell”})-eq $null)
{
Add-PsSnapin Microsoft.SharePoint.PowerShell | Out-Null
}
<#====================================================================
Get SharePoint variables
====================================================================#>
Add-Module(“C:\Temp\SharePoint2013\GetVariables.ps1”)
function Add-Module($strFileName){
Write-Host “Attempting to run ” $strFileName
If (Test-Path $strFileName){
&($strFileName)
}Else{
Write-Host “Cannot locate” $strFileName
}
}
#Enables Enterprise Search. Needs Usage Service Application to be created first or creates one itself if missing.
$ServiceApplicationName = “Search Service Application”
$ServiceName = “Search Service Application”
$IndexLocation = $SearchIndexLocation #Folder has to be empty AND has to exist!
$DatabaseName = “Search Service Application”
$spAppPoolName = “Search Service Application Pool”
$spContentAccessAcc = $CrawlAcc
$spContentAccessPwd = $CrawlPassword
$ServerName=$SearchServerName
#Get default SQL server
$DefaultDatabaseServer = (Get-SPDatabase | ? { $_.Type -eq “Configuration Database” }).NormalizedDataSource
#Get SQL server instance for the User Profile Application, Social,
$DatabaseServer = $SearchServerInstance
Write-Host “”
Write-Host “========================================================”
Write-Host “SharePoint 2013 – Adding ‘$ServiceApplicationName’…”
Write-Host “To SharePoint Server” $ServerName
Write-Host “Application Pool Name -” $spAppPoolName
Write-Host “Index Location ” $IndexLocation
Write-Host “Application Pool Account” $SearchAppPoolAcc
Write-Host “Crawl Account” $CrawlAcc
Write-Host “========================================================”
Write-Host “”
Start-SPAssignment -Global | Out-Null
try
{
#Check for existing service application and proxy
$ExistingServiceApp = Get-SPServiceApplication | where-object {$_.Name -eq $ServiceApplicationName}
if ($ExistingServiceApp -eq $null)
{
mkdir -Path $SearchIndexLocation -Force | Out-Null
Write-Host “- Creating ‘$ServiceApplicationName'”
#Start service instances
write-host “- Starting service instances”
Start-SPEnterpriseSearchServiceInstance $ServerName
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $ServerName
#Check if managed account already exist, if not exit
$spManagedAccount = Get-SPManagedAccount -Identity $SearchAppPoolAcc -ErrorAction SilentlyContinue
if ($spManagedAccount -eq $null)
{
Write-Host -f Red “- ” $ServiceName ” Managed Account – Unable to retrieve managed account” $SearchAppPoolAcc
exit -1
}
#Check if managed account already exist, if not exit
$spManagedAccount = Get-SPManagedAccount -Identity $SearchAppPoolAcc -ErrorAction SilentlyContinue
if ($spManagedAccount -eq $null)
{
Write-Host -f Red “- ” $ServiceName ” Managed Account – Unable to retrieve managed account” $SearchAppPoolAcc
exit -1
} $ApplicationPool = Get-SPServiceApplicationPool -Identity $spAppPoolName -ErrorAction SilentlyContinue
if ($ApplicationPool -eq $null)
{
New-SPServiceApplicationPool -Name $spAppPoolName -Account $spManagedAccount | Out-Null
}
else
{
Set-SPServiceApplicationPool $ApplicationPool -Account $spManagedAccount | Out-Null
}
#Search Application
Write-Host “- Creating Search Service Application”
$ServiceApplication = New-SPEnterpriseSearchServiceApplication -Name $ServiceApplicationName -ApplicationPool $spAppPoolName -DatabaseServer $DatabaseServer -DatabaseName $DatabaseName
#Create proxy
Write-Host “- Creating ‘$ServiceApplicationName’ proxy”
New-SPEnterpriseSearchServiceApplicationProxy -Name “$ServiceApplicationName Proxy” -SearchApplication $ServiceApplicationName | Out-Null
#Get the search instance
$searchInstance = Get-SPEnterpriseSearchServiceInstance $ServerName
#Topology
Write-Host “- Creating new search topology”
$InitialSearchTopology = $ServiceApplication | Get-SPEnterpriseSearchTopology -Active
$SearchTopology = $ServiceApplication | New-SPEnterpriseSearchTopology
#Administration + Processing Components
Write-Host “- Creating Administration Component”
New-SPEnterpriseSearchAdminComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance | Out-Null
Write-Host “- Creating Analytics Processing Component”
New-SPEnterpriseSearchAnalyticsProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance | Out-Null
Write-Host “- Creating Content Processing Component”
New-SPEnterpriseSearchContentProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance | Out-Null
Write-Host “- Creating Query Processing Component”
New-SPEnterpriseSearchQueryProcessingComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance | Out-Null
#Crawl
Write-Host “- Creating Crawl Component”
New-SPEnterpriseSearchCrawlComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance | Out-Null
#Index (Query)
Write-Host “- Creating Query Component”
#$IndexPartition= 1 #(Get-SPEnterpriseSearchIndexPartition -QueryTopology $SearchTopology)
New-SPEnterpriseSearchIndexComponent -SearchTopology $SearchTopology -SearchServiceInstance $searchInstance -RootDirectory $IndexLocation | Out-Null #-IndexPartition $IndexPartition
#Activates a Search Topology and requires all components attached to a topology
Write-Host “- Activating new Search Topology”
$SearchTopology | Set-SPEnterpriseSearchTopology
try
{
$InitialSearchTopology.Synchronize()
}
catch { }
Write-Host “- Waiting for the old crawl topology to become inactive..” -NoNewline
do {
Write-Host -NoNewline .;Start-Sleep 6;} while ($InitialSearchTopology.State -ne “Inactive”)
$InitialSearchTopology | Remove-SPEnterpriseSearchTopology -Confirm:$false
Write-Host
#Set Content Access account
Write-Host “- Setting Content Access Account”
$c = New-Object Microsoft.Office.Server.Search.Administration.Content($ServiceApplication)
$c.SetDefaultGatheringAccount($spContentAccessAcc, (ConvertTo-SecureString $spContentAccessPwd -AsPlainText -force))
Write-Host “- Done creating ‘$ServiceApplicationName’.`n”
}
}
catch { write-Output $_ }
Stop-SPAssignment -Global | Out-Null