Restore Timer Service in SharePoint server using PowerShell

No comments

 2,165 total views,  4 views today

Timer service is commissioned with each server connected to the SharePoint farm, however, we might see that the timer service is not commissioned with some of the servers connected to the farm – this we can check by navigating to SharePoint Central Administration site -> Monitoring -> Review Job Definitions. In such scenarios, we need to restore the timer service in the given faulty server. In this article, we will learn how to restore the SharePoint Timer Job service in the server using the PowerShell script.


####The below script is used to restore the SharePoint Timer Job service.#############################

cls

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

$fileName = "RestoreTimerJobService_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 restore the timer job service in SharePoint on-premise##################################

Try
{

#Pass the application server name,on which you want to restore the timer service, however you can pass any server name.
$serverName=Get-SPServer -Identity "<name of application server or any other server>"
$timerService = $serverName.ServiceInstances | ? { $_.GetType().Name -like "*sptimerservice*" } | Select -First 1
$timerService.AllowContentDatabaseJobs = $true
$timerService.AllowServiceJobs = $true
$timerService.Update()

}
Catch
{

$ErrorMessage = $_.Exception.Message +"in starting the central administration service in SharePoint on-premise!:"
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}

##############The below code is used to restore the timer job service in SharePoint on-premise - ends here########################


Summary

Thus, in this article, we have learned about how to restore the timer job service in the SharePoint server 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