Monday, February 11, 2013

Disable All SharePoint Timer Jobs Console Application

I found I needed to disable all the timer jobs in SharePoint 2010. Since there are over 100 jobs, I wrote a quick console application to disable them all for me. Below is the code. You can alter line 34 to job.IsDisabled = false; to make this application enable all the SharePoint timer jobs instead.

Note: Be sure to change the Target Framework of the project to .NET Framework 3.5.
using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace DisableTimerJobs
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {         
                SPFarm farm = SPFarm.Local; 
                //Get all SharePoint Web services 
                SPWebService service = farm.Services.GetValue<SPWebService>("");

                int Count = 0;

                foreach (SPWebApplication webapp in service.WebApplications)
                {

                    foreach (SPJobDefinition job in webapp.JobDefinitions)
                    {

                        //this example is to run on one service only
                        //if (job.Name == "SchedulingNotification")
                        //{
                            Count++;

                            try
                            {
                                //disable all jobs
                                job.IsDisabled = true;
                                job.Update();                               

                                Console.WriteLine(Count + ") " + job.Name + ": ");

                                if (job.IsDisabled == true)
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                    Console.WriteLine("Disabled");
                                    Console.ResetColor();
                                }
                                else
                                {
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.WriteLine("Enabled");
                                    Console.ResetColor();
                                }                                
                               
                            }
                            catch (Exception ex)
                            {                                
                                Console.WriteLine(Count + ") " + job.Name + ": ");
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("ERROR: ");
                                Console.WriteLine(ex.ToString());
                                Console.ResetColor();
                                Console.ReadLine();
                            }                            
                            
                        //}
                    }                   
                    
                }
            }

            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: ");
                Console.WriteLine("An error has occured: ");
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                Console.ReadLine();
            }

            Console.WriteLine("Done. Press enter to exit.");
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment