Re-Provisioning SharePoint Search Service Application using PowerShell

No comments

 3,566 total views,  14 views today

In this article, we will learn about how to re-provision the SharePoint search service application using the PowerShell script. Many times, we may be in need that needs to re-provision the search service application or any other service applications that got corrupted or not working anymore due to some technical reasons. For example, if you don’t find the “Indexing Schedule Manager on <Server Name>” timer job on your farm, you must re-provision the search service application.

Step1:

Using the below PowerShell command get all service applications from the farm:


Get-SPServiceApplication

The command will list out all service applications with their name and ID from the SharePoint farm.

Step 2:

Now, get the specific service application by the ID parameter or name like below:



####The below script is used to re-provision the SharePoint search service application.

cls

$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null)
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$fileName = "ReProvisionSharePointSSA_using_PowerShell"

#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
##############The below code is used to re-provison the search service Application in SharePoint on-premise##################################
#Get-SPServiceApplication -Identity GUID ID, example shown below:

Try
{

$ssa=Get-SPServiceApplication -Identity e2c2be70-6382-4ce7-8a55-ae7dadff5786

#or

#Get-SPServiceApplication -Name service application name, example shown below:

$ssa=Get-SPServiceApplication -Name "Search Service Application"

#Now make sure that the $ssa variable returning the correct object by typing the $ssa in the next line command.
#Once you see the that $ssa is returning the correct search service application details, run the below provision method.
$ssa.Provision()
}
catch
{
 $ErrorMessage = $_.Exception.Message +"in re-provisioning the SSA in SharePoint on-premise!:" 
 Write-Host $ErrorMessage -BackgroundColor Red
 Write-Log $ErrorMessage 
}

##############The below code is used to re-provison the search service Application in SharePoint on-premise - ends here##################################

Note:

  • In the above example, the way we have re-provisioned the SharePoint search service application, we can re-provision any service application from the SharePoint farm, just we need to pass the correct service application name or GUID in the Get-SPServiceApplication command.
  • Until it is needed (or corrupted), do not re-provision any service application.
  • First, run the above script or command in the test server before running in the prod server.

Summary:

Thus, in this article, we have learned about how to re-provision the SharePoint search service application using PowerShell script or how to re-provision any SharePoint service applications using the PowerShell script.

See Also:

You may also like the following SharePoint PowerShell tutorials:

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:

Buy SharePoint Online & Office 365 Administration eBook