Introduction

This article shows how to get the timer job status and histories using PowerShell scripts.

Timer Jobs:


A timer job contains a definition of the service to run and specifies how frequently the service is started. The SharePoint 2010 Timer service (SPTimerv4) runs timer jobs. Many features in SharePoint rely on timer jobs to run services according to a schedule.

Timer job reports:

Now I will show you how to export the timer job report into a CSV file. Look at the following code that gets you the information:

  1. Function TimerJobReport()
  2. {
  3. $Output = $scriptBase + "\" + "TimerJobReport.csv";
  4. "Name" + "," + "Status" + "," + "LastRun" + "," + "Schedule" | Out-File -Encoding Default -FilePath $Output;
  5. write-host "Generating TimerJob generic report" -fore yellow
  6. $TimerJobs = get-sptimerjob
  7. foreach($TimerJob in $Timerjobs)
  8. {
  9. $TimerJob.name + "," + $TimerJob.status + "," + $TimerJob.lastruntime + "," + $TimerJob.schedule | Out-File -Encoding Default -Append -FilePath $Output;
  10. }
  11. write-host "TimerJob genric report collected and placed under " $Output -fore green
  12. }

When the preceding function is called you get an output in a CSV file (TimerJobReport.csv) that has the preceding stated information on all the timer jobs in your SharePoint Farm.

Timer job Latest history

Now I will show you how to get the latest (last run) history for all the timer jobs in the SharePoint farm. The following code gets you the following information for all the timer jobs in the SharePoint Farm and outputs it to a CSV file (TimerJobHistoryReport.csv).

  1. Function TimerJobHistory()
  2. {
  3. $Output1 = $scriptBase + "\" + "TimerJobHistoryReport.csv";
  4. "Name" + "," + "Status" + "," + "ServerName" + "," + "WebApplicationName" + "," + "ErrorMessage" | Out-File -Encoding Default -FilePath $Output1;
  5. write-host "Generating TimerJob history report" -fore yellow
  6. $TimerJobs = get-sptimerjob
  7. foreach($TimerJob in $Timerjobs)
  8. {
  9. $JobHistories = $TimerJob.historyentries
  10. foreach($Jobhistory in $JobHistories)
  11. {
  12. if($TimerJob.lastruntime.ToUniversalTime() -eq $JobHistory.starttime)
  13. {
  14. $TimerJob.Name + "," + $Jobhistory.status + "," + $Jobhistory.servername + "," + $Jobhistory.WebApplicationName + "," + $Jobhistory.ErrorMessage | Out-File -Encoding Default -Append -FilePath $Output1;
  15. }
  16. }
  17. }
  18. write-host "TimerJob history report generated and placed under " $output1 -fore green
  19. }

In the preceding function you need to match the last run time with the job history start time to get the latest history for the timer job.

Specific Timer job history

Now I will show you how to get history for a specific timer job and how many latest histories that you want to look at. The following script generates a report for a given specific timer job and it is input from the user to show how much history data they would need to generate the given timer job. The following code helps to do that and exports the output to a CSV file (SpecificTimerJobHistoryReport.csv).

  1. Function SpecificTimerJob()
  2. {
  3. $Output2 = $scriptBase + "\" + "SpecificTimerJobHistoryReport.csv";
  4. "Name" + "," + "Status" + "," + "ServerName" + "," + "TimerJobStartTime" + "," + "WebApplicationName" + "," + "ErrorMessage" | Out-File -Encoding Default -FilePath $Output2;
  5. $TimerJobName = read-host "Enter the timer job name "
  6. $Timerjob = get-sptimerjob -identity $TimerJobName
  7. $jobHistories = @($timerjob.historyentries)
  8. $HowManyHistory = read-host "Please enter the number of histories that you want to return for this timerjob "
  9. for($i = 0 ; $i -le $HowManyHistory; $i++)
  10. {
  11. $TimerJob.Name + "," + $jobHistories[$i].status + "," + $jobHistories[$i].servername + "," + $jobHistories[$i].StartTime + "," + $jobHistories[$i].WebApplicationName + "," + $jobHistories[$i].ErrorMessage | Out-File -Encoding Default -Append -FilePath $Output2;
  12. }
  13. break;
  14. }

The preceding function requires input from the user on the following 2 things:

Complete code

Please find the following complete code for getting the various reports on timer jobs.

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\TimerJobReportPatch-$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. #Deleting any .rtf files in the scriptbase location
  10. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  11. if($FindRTFFile)
  12. {
  13. foreach($file in $FindRTFFile)
  14. {
  15. remove-item $file
  16. }
  17. }
  18. start-transcript $logfile
  19. Function TimerJobReport()
  20. {
  21. $Output = $scriptBase + "\" + "TimerJobReport.csv";
  22. "Name" + "," + "Status" + "," + "LastRun" + "," + "Schedule" | Out-File -Encoding Default -FilePath $Output;
  23. write-host "Generating TimerJob generic report" -fore yellow
  24. $TimerJobs = get-sptimerjob
  25. foreach($TimerJob in $Timerjobs)
  26. {
  27. $TimerJob.name + "," + $TimerJob.status + "," + $TimerJob.lastruntime + "," + $TimerJob.schedule | Out-File -Encoding Default -Append -FilePath $Output;
  28. }
  29. write-host "TimerJob genric report collected and placed under " $Output -fore green
  30. }
  31. Function TimerJobHistory()
  32. {
  33. $Output1 = $scriptBase + "\" + "TimerJobHistoryReport.csv";
  34. "Name" + "," + "Status" + "," + "ServerName" + "," + "WebApplicationName" + "," + "ErrorMessage" | Out-File -Encoding Default -FilePath $Output1;
  35. write-host "Generating TimerJob history report" -fore yellow
  36. $TimerJobs = get-sptimerjob
  37. foreach($TimerJob in $Timerjobs)
  38. {
  39. $JobHistories = $TimerJob.historyentries
  40. foreach($Jobhistory in $JobHistories)
  41. {
  42. if($TimerJob.lastruntime.ToUniversalTime() -eq $JobHistory.starttime)
  43. {
  44. $TimerJob.Name + "," + $Jobhistory.status + "," + $Jobhistory.servername + "," + $Jobhistory.WebApplicationName + "," + $Jobhistory.ErrorMessage | Out-File -Encoding Default -Append -FilePath $Output1;
  45. }
  46. }
  47. }
  48. write-host "TimerJob history report generated and placed under " $output1 -fore green
  49. }
  50. Function SpecificTimerJob()
  51. {
  52. $Output2 = $scriptBase + "\" + "SpecificTimerJobHistoryReport.csv";
  53. "Name" + "," + "Status" + "," + "ServerName" + "," + "TimerJobStartTime" + "," + "WebApplicationName" + "," + "ErrorMessage" | Out-File -Encoding Default -FilePath $Output2;
  54. $TimerJobName = read-host "Enter the timer job name "
  55. $Timerjob = get-sptimerjob -identity $TimerJobName
  56. $jobHistories = @($timerjob.historyentries)
  57. $HowManyHistory = read-host "Please enter the number of histories that you want to return for this timerjob "
  58. for($i = 0 ; $i -le $HowManyHistory; $i++)
  59. {
  60. $TimerJob.Name + "," + $jobHistories[$i].status + "," + $jobHistories[$i].servername + "," + $jobHistories[$i].StartTime + "," + $jobHistories[$i].WebApplicationName + "," + $jobHistories[$i].ErrorMessage | Out-File -Encoding Default -Append -FilePath $Output2;
  61. }
  62. break;
  63. }
  64. write-host "########################################################################################################" -fore cyan
  65. write-host "Enter 1 to get SP Timer job generic reports" -fore green
  66. write-host "Enter 2 to get specific SP Timer job report " -fore green
  67. write-host "########################################################################################################" -fore cyan
  68. $option = read-host "Enter the option "
  69. switch($option)
  70. {
  71. 1{
  72. TimerJobReport
  73. TimerJobHistory
  74. }
  75. 2{
  76. SpecificTimerJob
  77. }
  78. }
  79. write-host "SCRIPT COMPLETED" -fore green
  80. stop-transcript

Execution procedure

Step 1: Copy the script to the execution folder.

Step 2: Launch the SharePoint Management Shell.

Step 3: Navigate to the execution folder path.

Step 4: Execute the TimerJobReport.ps1 file.

TimerJobReport

Enter “1” to get the generic timer job reports. Option “1” captures the following information:

generic timer job reports

Once the script completes you get the outputs in 2 CSV files. One (TimerJobReport.csv) for the generic timer job report and another (TimerJobHistoryReport.csv) for the latest history for all the timer jobs in the SP Farm.

The output looks as in the following:

  1. TimerJobReport.csv file.

    csv file

  2. TimerJobHistoryReport.csv file.

    TimerJobHistoryReport

    Error message is only displayed when you encounter failures.

Enter “2” to get specific timer job report.

get specific timer job report

Enter the timer job name for which you want to list the latest number of histories.

You get the output in a CSV file (SpecificTimerJobHistoryReport.csv) that is located under the location where the .ps1 file is placed. The output CSV file has the following details.

details

Conclusion

Thus this article outlined how to get the timer job reports in a SharePoint 2010 Farm.