locked
Failed timer jobs via powershell RRS feed

  • Question

  • Hi Friends,

    Is it possible to get an output of the failed timer jobs in the Central Admin via powershell? If yes, could anybody please share the script?


    Shonilchi..

    Wednesday, November 14, 2012 12:09 PM

Answers

  • How about this then ;-)

    $f = get-spfarm
    $ts = $f.TimerService
    #Display all jobs that haven't succeeded (I.e. Paused)
    $ts.JobHistoryEntries | ?{$_.Status -ne "Succeeded"} | Ft JobDefinitionTitle,Status
    #Display only failed jobs
    $ts.JobHistoryEntries | ?{$_.Status -eq "Failed"} | Ft JobDefinitionTitle,Status
    #Display more information
    $ts.JobHistoryEntries | ?{$_.Status -eq "Failed"} | fl


    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Shonilchi Monday, November 19, 2012 3:03 PM
    Monday, November 19, 2012 1:14 PM
  • Hey Shonilchi,

    I've uploaded a script to the Technet Gallery that exports the information to a CSV file. The WebApplication appears to always be empty in the history list, so I've omittd it from the script).

    The link to it is here: http://gallery.technet.microsoft.com/sharepoint/Export-Failed-Timer-Jobs-ee69a53d

    Hope it helps, Matt


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Shonilchi Monday, November 26, 2012 12:12 PM
    Wednesday, November 21, 2012 9:09 PM
  • Finally got it working.  Here is the script:

    $items = New-Object psobject 
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "Schedule" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "WebApplication" -value "" ;

    $webapplications = Get-SPWebApplication
    $jobCollection = @();
    foreach($wa in $webapplications)
    {
    $jd = $wa.JobDefinitions;
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication=$job.WebApplication.Url; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }
    }

    #Add the farm jobs
    $f = get-spfarm
    $ts = $f.TimerService
    $jd = $ts.JobDefinitions
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication="Farm"; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }

    $jobCollection | ?{$_.Webapplication -like "Your site URL*"} | Format-List Title,Schedule,Webapplication > c:\jobs.txt


    Shonilchi..

    • Marked as answer by Shonilchi Tuesday, August 6, 2013 12:25 PM
    Tuesday, August 6, 2013 12:25 PM

All replies

  • Is this what you're after? It lists all the timer jobs that haven't succeeded for a webapplication

    $wa = Get-SPWebApplication http://mywebapp
    $wa.JobHistoryEntries | ?{$_.Status -ne "Succeeded"} | Ft JobDefinitionTitle,Status


    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Wednesday, November 14, 2012 12:22 PM
  • Did it help, or were you looking for something different?

    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Thursday, November 15, 2012 10:18 AM
  • Hi Matthew,

    The script that you have provided is for a webapplication, but in the Central administration, we can find the failed timer job definitions for the entire farm where we cannot filter down by the web application.


    Shonilchi..

    Monday, November 19, 2012 12:54 PM
  • How about this then ;-)

    $f = get-spfarm
    $ts = $f.TimerService
    #Display all jobs that haven't succeeded (I.e. Paused)
    $ts.JobHistoryEntries | ?{$_.Status -ne "Succeeded"} | Ft JobDefinitionTitle,Status
    #Display only failed jobs
    $ts.JobHistoryEntries | ?{$_.Status -eq "Failed"} | Ft JobDefinitionTitle,Status
    #Display more information
    $ts.JobHistoryEntries | ?{$_.Status -eq "Failed"} | fl


    Regards, Matthew
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Shonilchi Monday, November 19, 2012 3:03 PM
    Monday, November 19, 2012 1:14 PM
  • Great script!! It works...  Now that it is working, is it possible to include a timestamp like for the past 1 week and also if the output is exactly as below it would be great!!


    Shonilchi..

    Monday, November 19, 2012 2:59 PM
  • Everything is possible with PowerShell ;-)

    $f = get-spfarm
    $ts = $f.TimerService
    $ts.JobHistoryEntries | ?{$_.Status -eq "Failed" -and $_.StartTime -gt ((get-date).AddDays(-7))} | Ft JobDefinitionTitle,Status,StartTime,EndTime,@{Label="TotalRunningTime"; Expression={$_.EndTime - $_.StartTime}}


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    Monday, November 19, 2012 3:59 PM
  • Thanks for the script Matthew, its working fine.. the requirement is inclusive of the below column names, which I am not able to get and in order:

    JobDefinitionTitle,Server, web application, duration, Status,StartTime,EndTime

    Getting this output in a CSV or XLS would help a great deal.


    Shonilchi..

    Tuesday, November 20, 2012 8:11 AM
  • Hey Shonilchi,

    I've uploaded a script to the Technet Gallery that exports the information to a CSV file. The WebApplication appears to always be empty in the history list, so I've omittd it from the script).

    The link to it is here: http://gallery.technet.microsoft.com/sharepoint/Export-Failed-Timer-Jobs-ee69a53d

    Hope it helps, Matt


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Marked as answer by Shonilchi Monday, November 26, 2012 12:12 PM
    Wednesday, November 21, 2012 9:09 PM
  • Thanks a lot Matthew!! ;)  It really helped..

    Shonilchi..

    Monday, November 26, 2012 12:12 PM
  • Superb matthew, :) Can i get a script to get a list of all timer jobs with the schedule, please let me know.

    ~SenjiVi

    Monday, July 8, 2013 11:35 AM
  • Hi SenjiVi,

    You can get a collection of all the timer jobs running on the farm and webapplications like this;

    $items = New-Object psobject 
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "Schedule" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "WebApplication" -value "" ;
    
    $webapplications = Get-SPWebApplication
    $jobCollection = @();
    foreach($wa in $webapplications)
    {
    $jd = $wa.JobDefinitions;
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication=$job.WebApplication.Url; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }
    }
    
    #Add the farm jobs
    $f = get-spfarm
    $ts = $f.TimerService
    $jd = $ts.JobDefinitions
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication="Farm"; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }

    The script defines a new object for storing job information. It then parses all the jobs in all webapplications, adding each SPTimerJob to a new object, adding each object to the collection of jobs. Once this has finished, you have a collection of "jobs" that you can filter or sort like any other PowerShell collection.

    $jobCollection | FT
    #Or
    $jobCollection | ?{$_.Webapplication -like "http://devmy101*"} | FT Title,Schedule,Webapplication -AutoSize


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos

    Tuesday, July 9, 2013 10:07 AM
  • Hi Matthew,

    I tried the above script; it just runs, meaning, am not getting an output.. Please help.. I require this in a CSV or XML.


    Shonilchi..

    Monday, July 29, 2013 3:08 PM
  • This script just runs, doesnot provide any output. can you please help


    ~SenjiVi

    Tuesday, August 6, 2013 12:18 PM
  • Finally got it working.  Here is the script:

    $items = New-Object psobject 
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "Schedule" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "WebApplication" -value "" ;

    $webapplications = Get-SPWebApplication
    $jobCollection = @();
    foreach($wa in $webapplications)
    {
    $jd = $wa.JobDefinitions;
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication=$job.WebApplication.Url; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }
    }

    #Add the farm jobs
    $f = get-spfarm
    $ts = $f.TimerService
    $jd = $ts.JobDefinitions
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication="Farm"; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }

    $jobCollection | ?{$_.Webapplication -like "Your site URL*"} | Format-List Title,Schedule,Webapplication > c:\jobs.txt


    Shonilchi..

    • Marked as answer by Shonilchi Tuesday, August 6, 2013 12:25 PM
    Tuesday, August 6, 2013 12:25 PM
  • Hi Shonilchi, 

    You haven't actually changed the script, other than to pipe the output of Format-Table to a text file. 

    The script (that I posted above) creates a collection of "jobs", which are stored in the $jobCollection variable. 

    The original code I posted above contained an example that filtered the collection based on a webapplication URL (which was http://devmy101), and then displayed the results using Format-Table.

    To create a CSV report using this collection, you would send the $jobCollection variable to the Export-CSV PowerShell cmdlet, like this:

    $jobCollection | Export-Csv -Path C:\temp\jobsreport.csv -NoTypeInformation

    If you wanted to create a CSV report of all the jobs that had failed on the farm, then this would be the complete code:

    $items = New-Object psobject 
    $items | Add-Member -MemberType NoteProperty -Name "Title" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "Schedule" -value "" ;
    $items | Add-Member -MemberType NoteProperty -Name "WebApplication" -value "" ;
    
    $webapplications = Get-SPWebApplication
    $jobCollection = @();
    foreach($wa in $webapplications)
    {
    $jd = $wa.JobDefinitions;
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication=$job.WebApplication.Url; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }
    }
    
    #Add the farm jobs
    $f = get-spfarm
    $ts = $f.TimerService
    $jd = $ts.JobDefinitions
    foreach($job in $jd)
    {
    $j = $items | Select-Object *; 
    $j.Title=$job.Title; 
    $j.WebApplication="Farm"; 
    $j.Schedule=$job.Schedule; 
    $jobCollection += $j
    }
    
    #Create the report and save it as jobsreport.csv in the C:\temp directory
    $jobCollection | Export-Csv -Path C:\temp\jobsreport.csv -NoTypeInformation


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos

    Tuesday, August 6, 2013 12:40 PM
  • Hi,

    I am trying to do the same for Content Deployment jobs. How to pull the failed jobs info for CD's.

    Thanks,

    Raj.

    Thursday, November 21, 2013 12:40 AM
  • This particular thread posting you led with the comment:

    > If you wanted to create a CSV report of all the jobs that had failed on the farm, then this would be the complete code:

    I don't see anything in this code that selects from the list of all timer jobs just the ones that failed.

    I presume this means that I misunderstand the code, since the code that is listed as the "complete" code appears, to me, to be the same as the earlier code that was posted to list all jobs on the farm.

    Am I missing something obvious?

    Friday, April 25, 2014 2:00 PM
  • Awesome script ...

    Please check below link

    http://parmeshwarjadhao.blogspot.in/2015/02/powershell-script-for-daily-failed-job.html

    Wednesday, March 25, 2015 6:50 AM
  • Just a quick thought, what would be the difference between what you guys suggest and the SharePoint Powershell Command of Get-SPTimerJob?


    $f = get-spfarm
    $ts = $f.TimerService

    The difference here its narrowing on the a single web application

    $wa = Get-SPWebApplication http://mywebapp
    $wa.JobHistoryEntries | ?{$_.Status -ne "Succeeded"} | Ft JobDefinitionTitle,Status

    This Gets all the timer jobs as well

    $timerjobs = Get-SPTimer
    $timerjobs.JobHistoryEntries | ?{$_.Status -ne "Succeeded"} | Ft JobDefinitionTitle,Status

    Just wondering which would be more efficient?




    pfcjt@hotmail.com

    Monday, July 25, 2016 1:31 PM
  • If you are looking to export the same list of failed jobs as shown in Central Administration, then I suggest using the following PowerShell:

    (Get-SPFarm).Services |
        % { $_.JobHistoryEntries | ? { $_.Status -eq "Failed" } } |
        sort StartTime |
        select JobDefinitionTitle, ServerName, WebApplicationName, StartTime, EndTime,
            ErrorMessage |
        Export-Csv Failed-Timer-Jobs.csv

    I initially attempted to use SPWebApplication.JobHistoryEntries and SPFarm.TimerService.JobHistoryEntries (based on the PowerShell examples provided previously), as follows:

    $failedWebAppJobs = Get-SPWebApplication -IncludeCentralAdministration |
        % { $_.JobHistoryEntries | ? { $_.Status -eq "Failed" } }
    
    $failedFarmJobs = ((Get-SPFarm).TimerService).JobHistoryEntries |
        ? { $_.Status -eq "Failed" }
    
    $failedTimerJobs = @()
    $failedTimerJobs += $failedWebAppJobs
    $failedTimerJobs += $failedFarmJobs
    
    $failedTimerJobs |
        sort StartTime |
        select JobDefinitionTitle, ServerName, WebApplicationName, StartTime, EndTime,
            ErrorMessage |
        Export-Csv Failed-Timer-Jobs.csv
    

    However, I discovered a large number of failed jobs listed in Central Administration were not included in that output. Specifically, the following failed jobs were omitted:

    • eDiscovery In-Place Hold Processing
    • Health Statistics Updating
    • Query Classification Dictionary Update for Search Application Search Service Application.
    • Search Custom Dictionaries Update
    • Search Health Monitoring - Trace Events
    • User Profile Service Application - User Profile Incremental Synchronization

    While enumerating the job history entries for each individual service in the farm is slower, at least you end up with the exact same list as what is displayed in Central Administration.

    Friday, September 30, 2016 4:57 PM