Powershell Script to Restart SharePoint Timer Service

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

Up Next
    Ebook Download
    View all
    Learn
    View all