[Fixed]: Restore the timer service in SharePoint Server using PowerShell script

No comments

 400 total views,  16 views today

As we know SharePoint timer job is commissioned in all servers connected to the SharePoint farm. If we go to the monitoring from the central administration site, there we could see due to some reasons for some of the servers, the timer job instances are not running or decommissioned which was supposed to be run. In this case how to bring back the failed timer job for that server.

In this article, we will learn how to restore the timer job service instance for the given server if it is not running or decommissioned using the PowerShell script.

In the below example, I have assumed that the timer job service is to running or decommissioned in the application server, where we want to restore the timer job service back to the server.


####The below script is used to restore the timer service in SharePoint server.

cls

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

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

Try
{
#Get the application server on which we want to restore the timer service
$server=Get-SPServer -Identity "Name of application server" #Here we could pass the server name where timer job service is not running.
$timerService = $server.ServiceInstances | ? { $_.GetType().Name -like "*sptimerservice*" } | Select -First 1
$timerService.AllowContentDatabaseJobs = $true
$timerService.AllowServiceJobs = $true
$timerService.Update()

}
Catch
{

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

}

##############The below code restore the timer 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