Migrating Scheduled Task Jobs

Finally retiring that old Windows Server 2003 machine? Need to migrate your scheduled tasks? Here is an easy solution.

One of our clients is finally migrating from Windows Server 2003 to Windows Server 2012 (don't judge) and I ran into an interesting problem. They have a bunch of jobs set up in the Task Scheduler that need to be migrated over, but the old machine uses a deprecated binary .job file format to store the job data, while the newer machine uses an xml-based file format.

Of course, there is a built-in solution for this problem: the schtasks program now has an /XML switch to export an xml file. However, that version of schtasks only works on the newer OSes, so you cant easily convert the scripts on your old machine.

Surely Google can help, right? It didnt take long to find several solutions like this that let you specify the remote host name of the older OS. In that case, you can use the newer schtasks (with xml export) but point it at the older machine and everything is easy-peasy.

But that only works if the two machines are on the same domain and can see each other. In my case, we were moving from one virtual hosting company to another, so the two machines could not interact in this way.

Back to Google and I find a long-winded but working solution that requires you to copy the old version of schtasks to the new machine, pipe some directory output to text file, manually create an Excel spreadsheet that gets saved as a .bat file and eventually execute schtasks on a copy of the old jobs. Whew - there had to be a better way! So I whipped one up.

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("username:");
            var username = Console.ReadLine();
            Console.WriteLine("password:");
            var password = Console.ReadLine();
            var files = Directory.GetFiles(@"c:\windows\tasks", "*.job");
            if (files == null || files.Count() == 0)
            {
                Console.WriteLine("No .job files found to process");
            }
            else
            {
                foreach (var file in files)
                {
                    var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
                    var arguments = String.Format("/change /TN {0} /RU {1} /RP {2}", fileNameWithoutExtension, username, password);
                    var psi = new ProcessStartInfo(@"schtasksXP", arguments);
                    psi.UseShellExecute = false;
                    Console.WriteLine(psi.FileName + " " + psi.Arguments);
                    var p = Process.Start(psi);
                    p.WaitForExit();
                }
            }
            Console.WriteLine("DONE");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
    }

To use it:

  • Copy your old .job files to C:\Windows\Tasks on the new machine
  • Download the .zip file and unzip the contents to a folder
  • Run TaskScheduleJobConverter.exe which will prompt you for a username & password to be used to execute the imported jobs

When the script completes, all of the old jobs will have been converted and imported into the new machine's Task Scheduler.

Download the program

@briandunnington


2020.11.25

Serializing JObject to camelCase

How to serialize a JSON.NET JObject to camelCase

View details »


2020.03.20

Azure Functions: Trigger Attribute Settings From Key Vault

How to read trigger attribute settings from Key Vault

View details »


2020.03.19

Azure Functions: Wildcard Routing

A quick tip to support wildcard routes in Azure Functions

View details »


2019.08.01

Azure Functions: Route Priority

Making Azure Functions route matching make more sense

View details »


2019.07.30

Azure Functions: Set Cosmos DB Trigger Connection String At Runtime

How to specify the trigger connection string at runtime and avoid storing secrets during local development

View details »


More Posts >