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:
- Function RestartTimerServiceInSPFarm([Microsoft.SharePoint.Administration.SPFarm]$farm)
- {
- Write-Host ""
- foreach($server in $farm.Servers)
- {
- foreach($instance in $server.ServiceInstances)
- {
- # If the server has the timer service then stop the service
- if($instance.TypeName -eq $timerServiceInstanceName)
- {
- [string]$serverName = $server.Name
-
- Write-Host "Stop " $timerServiceName " service on server: " $serverName -fore yellow
-
- $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
- $serviceInternalName = $service.Name
- sc.exe \\$serverName stop $serviceInternalName > $null
-
- # Wait until this service has actually stopped
- write-host "Waiting for '$timerServiceName' service on server: " $serverName " to be stopped" -fore yellow
- do
- {
- Start-Sleep 1
- Write-Host -foregroundcolor DarkGray -NoNewLine "."
- $service = Get-WmiObject -ComputerName $serverName Win32_Service-Filter "DisplayName='$timerServiceName'"
- }
- while ($service.State -ne "Stopped")
- write-host " '$timerServiceName' service on server: " $serverName " stopped successfully" -fore green
-
-
-
- Write-Host "Start " $timerServiceName " service on server: " $serverName -fore yellow
-
- $service = Get-WmiObject -ComputerName $serverName Win32_Service -Filter "DisplayName='$timerServiceName'"
- $serviceInternalName = $service.Name
- sc.exe \\$serverName start $serviceInternalName > $null
-
- # Wait until this service starts running
- write-host "Waiting for " $timerServiceName " service on server: " $serverName " to be started" -fore yellow
- do
- {
- Start-Sleep 1
- Write-Host -foregroundcolor DarkGray -NoNewLine "."
- $service = Get-WmiObject -ComputerName $serverName Win32_Service-Filter "DisplayName='$timerServiceName'"
- }
- while ($service.State -ne "Running")
- write-host $timerServiceName " service on server: " $serverName " started successfully" -fore green
- write-host ""
- #break;
- }
- }
- }
-
- Write-Host ""
- }
Complete Code
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.