How to Get SharePoint 2010 Timer Job Reports and Histories

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:

  • Timer job name
  • Timer job status
  • Timer job last run time
  • Timer job schedule
  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  

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).

  • Timer job name
  • Timer job status
  • Server name
  • Web application name
  • Error message for failures
  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  

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;  

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

  • Timer job name
  • How many latest histories they need

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

  • Report on all the timer jobs in the SP Farm.

  • The latest history for all the timer jobs in the SP Farm.

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.

Next Recommended Readings