Introduction

Timer Service

A SharePoint Farm requires a mechanism to run tasks necessary to provide its services. These tasks include updating components of the Farm such as servers and services and updating data and configuration in Farm databases. To run these tasks, SharePoint provides its own scheduled tasks management service, manifested as Timer Service instances installed on every SharePoint server in the Farm.

Consider a scenario where you need to restart the SharePoint timer service in all the servers in the Farm. the manual work can be time consuming and PowerShell becomes handy here.

The following piece of code restarts the SharePoint timer service in the SharePoint Farm:
  1. Function RestartTimerServiceInSPFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)
  2. {
  3. Write-Host ""
  4. foreach($server in $farm.Servers)
  5. {
  6. foreach($instance in $server.ServiceInstances)
  7. {
  8. # If the server has the timer service then stop the service
  9. if($instance.TypeName -eq $timerServiceInstanceName)
  10. {
  11. [string]$serverName = $server.Name
  12. Write-Host "Stop " $timerServiceName " service on server: " $serverName -fore yellow
  13. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  14. $serviceInternalName = $service.Name
  15. sc.exe \\$serverName stop $serviceInternalName > $null
  16. # Wait until this service has actually stopped
  17. write-host "Waiting for '$timerServiceName' service on server: " $serverName " to be stopped" -fore yellow
  18. do
  19. {
  20. Start-Sleep 1
  21. Write-Host -foregroundcolor DarkGray -NoNewLine "."
  22. $service = Get-WmiObject -ComputerName $serverName Win32_Service-Filter "DisplayName='$timerServiceName'"
  23. }
  24. while ($service.State -ne "Stopped")
  25. write-host " '$timerServiceName' service on server: " $serverName " stopped successfully" -fore green
  26. Write-Host "Start " $timerServiceName " service on server: " $serverName -fore yellow
  27. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  28. $serviceInternalName = $service.Name
  29. sc.exe \\$serverName start $serviceInternalName > $null
  30. # Wait until this service starts running
  31. write-host "Waiting for " $timerServiceName " service on server: " $serverName " to be started" -fore yellow
  32. do
  33. {
  34. Start-Sleep 1
  35. Write-Host -foregroundcolor DarkGray -NoNewLine "."
  36. $service = Get-WmiObject -ComputerName $serverName Win32_Service-Filter "DisplayName='$timerServiceName'"
  37. }
  38. while ($service.State -ne "Running")
  39. write-host $timerServiceName " service on server: " $serverName " started successfully" -fore green
  40. write-host ""
  41. #break;
  42. }
  43. }
  44. }
  45. Write-Host ""
  46. }
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\SPTimerServiceRestartPatch-$LogTime.rtf"
  3. # Add SharePoint PowerShell Snapin
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
  5. Add-PSSnapin Microsoft.SharePoint.Powershell
  6. }
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  8. Set-Location $scriptBase
  9. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
  10. $TestLogFolder = test-path -path $scriptbase\Logs
  11. if($TestLogFolder)
  12. {
  13. write-host "The log folder already exist in the script location" -fore yellow
  14. $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
  15. if($clearlogfolder -eq 'y')
  16. {
  17. write-host "The user choosen to clear the log folder" -fore yellow
  18. write-host "Clearing the log folder" -fore yellow
  19. remove-item $scriptbase\Logs\* -recurse -confirm:$false
  20. write-host "Log folder cleared" -fore yellow
  21. }
  22. else
  23. {
  24. write-host "The user choosen not to clear the log files" -fore yellow
  25. }
  26. }
  27. else
  28. {
  29. write-host "Log folder does not exist" -fore yellow
  30. write-host "Creating a log folder" -fore yellow
  31. New-Item $Scriptbase\Logs -type directory
  32. write-host "Log folder created" -fore yellow
  33. }
  34. #moving any .rtf files in the scriptbase location
  35. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  36. if($FindRTFFile)
  37. {
  38. write-host "Some old log files are found in the script location" -fore yellow
  39. write-host "Moving old log files into the Logs folder" -fore yellow
  40. foreach($file in $FindRTFFile)
  41. {
  42. move-item -path $file -destination $scriptbase\logs
  43. }
  44. write-host "Old log files moved successfully" -fore yellow
  45. }
  46. start-transcript $logfile
  47. $global:timerServiceName = "SharePoint 2010 Timer"
  48. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
  49. # Get the local farm instance
  50. [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
  51. Function RestartTimerServiceInSPFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)
  52. {
  53. Write-Host ""
  54. foreach($server in $farm.Servers)
  55. {
  56. foreach($instance in $server.ServiceInstances)
  57. {
  58. # If the server has the timer service then stop the service
  59. if($instance.TypeName -eq $timerServiceInstanceName)
  60. {
  61. [string]$serverName = $server.Name
  62. Write-Host "Stop " $timerServiceName " service on server: " $serverName -fore yellow
  63. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  64. $serviceInternalName = $service.Name
  65. sc.exe \\$serverName stop $serviceInternalName > $null
  66. # Wait until this service has actually stopped
  67. write-host "Waiting for '$timerServiceName' service on server: " $serverName " to be stopped" -fore yellow
  68. do
  69. {
  70. Start-Sleep 1
  71. Write-Host -foregroundcolor DarkGray -NoNewLine "."
  72. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  73. }
  74. while ($service.State -ne "Stopped")
  75. write-host " '$timerServiceName' service on server: " $serverName " stopped successfully" -fore green
  76. Write-Host "Start " $timerServiceName " service on server: " $serverName -fore yellow
  77. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  78. $serviceInternalName = $service.Name
  79. sc.exe \\$serverName start $serviceInternalName > $null
  80. # Wait until this service starts running
  81. write-host "Waiting for " $timerServiceName " service on server: " $serverName " to be started" -fore yellow
  82. do
  83. {
  84. Start-Sleep 1
  85. Write-Host -foregroundcolor DarkGray -NoNewLine "."
  86. $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
  87. }
  88. while ($service.State -ne "Running")
  89. write-host $timerServiceName " service on server: " $serverName " started successfully" -fore green
  90. write-host ""
  91. #break;
  92. }
  93. }
  94. }
  95. Write-Host ""
  96. }
  97. RestartTimerServiceInSPFarm $farm
  98. write-host ""
  99. write-host "SCRIPT COMPLETED" -fore green
  100. stop-transcript
Execution Procedure

Step 1: Download and copy the script to the SharePoint server.
Step 2: Navigate to the script path.
Step 3: Execute the script.



Conclusion

Thus this article explains how to restart the SharePoint timer service using a PowerShell script.