Wednesday 9 November 2016

How to create and configure a Search service application in SharePoint Server 2016 Using PowerShell

Today we are writing about how to create and configure a search service application in SharePoint 2016 using PowerShell ISE.
See the below URL for more details for search Service for SharePoint:
Below are the steps to create a Search service application
  • We need to verify that the user account that is performing this procedure is a member of the Farm Administrators group for the farm for which you want to create the service application.
  • Click on search button on the server and enter PowerShell ISE and run as administrator.
  • It will open with blank page, If written space is unavailable we need to click on CTRL+N and copy below script with required modification.


#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"
$ServerName = "SP2016" #Your server name
$IndexLocation = "C:\Index" #Folder has to be empty AND has to exist!
$DatabaseName = "spServiceApp-Search" # Your Database Name
$spAppPoolName = "Search Service Application Pool Account"# Your App Pool name
$spAppPoolAcc = "spsolutionstips\spadmin" #your farm administrator login
$spContentAccessAcc = "spsolutionstips\spadmin" #your farm administrator login
$spContentAccessPwd = "!@#$%^SDFFGRerefdf" # Your password

Write-Host "SharePoint 2013 - '$ServiceApplicationName'..."

#Allows to use SharePoint cmdlets from inside the Windows PowerShell command window
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
Start-SPAssignment -Global | Out-Null

#Get default SQL server


$DatabaseServer = (Get-SPDatabase | ? { $_.Type -eq "Configuration Database" }).NormalizedDataSource

try
{
   #Check for existing service application and proxy
   $ExistingServiceApp = Get-SPServiceApplication | where-object {$_.Name -eq $ServiceApplicationName}

if ($ExistingServiceApp -eq $null)
   {
       mkdir -Path $IndexLocation -Force | Out-Null

Write-Host " - Creating '$ServiceApplicationName'"

       #Start service instances
       write-host " - Starting service instances"
       Start-SPEnterpriseSearchServiceInstance $ServerName
       Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $ServerName

       #Check if application pool already exist, if not create it
       $spManagedAccount = Get-SPManagedAccount -Identity $spAppPoolAcc
       $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"
}
else
   {
       #Write-Host " - '$ServiceApplicationName' already exists."
       Write-Host " - Removing '$ServiceApplicationName'..."
       Remove-SPServiceApplication $ExistingServiceApp -removedata -Confirm:$false

       #Proxy is NOT automatically deleted
       $ExistingServiceAppProxy = Get-SPServiceApplicationProxy | where-object {$_.Name -eq "$ServiceApplicationName Proxy"}
       if ($ExistingServiceAppProxy -ne $null)
       {
           Write-Host " - Removing '$ServiceApplicationName proxy'..."
           Remove-SPServiceApplicationProxy $ExistingServiceAppProxy -Confirm:$false
       }
       Write-Host " - Stopping service instance..."
       Stop-SPEnterpriseSearchServiceInstance $ServerName
       Stop-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $ServerName

       Remove-Item -Recurse -Force -LiteralPath $IndexLocation -ErrorAction SilentlyContinue
   }
}
catch { write-Output $_ }


Stop-SPAssignment -Global | Out-Null
cmd.exe /c pause

  • Changes are required on highlighted line.
  • Once all changes done, click on run button or press F5 on the key board, scripting will start running. Wait for some time till we are getting successful message on the PowerShell script page.
  • Once run successfully, Open CA and verify from app management service is created.
  • We can verify by clicking on Database server and verify below Database has been created.


Thanks ☺

4 comments: