Export Scheduled Tasks

Export Scheduled TasksWhile the new Task Scheduler 2.0 that came along around the time of Windows Vista and Windows Server 2008 brought along many improvements over the previous Windows Task Scheduler, there are still plenty of features lacking, especially in the tools that you get out of the box – specifically taskschd.msc and schtasks.exe. Fortunately there’s a fairly decent Task Scheduler API you can leverage. Even better, there’s an open source Task Scheduler Managed Wrapper available on CodePlex that gives you the ability to write your own custom utilities around the Windows Task Scheduler in .NET. Here we’ll show a small example utility you can build to perform a common task – export scheduled tasks to their underlying XML definitions in one bulk operation.

First, download the latest release of the Task Scheduler Managed Wrapper from CodePlex and extract the Microsoft.Win32.TaskScheduler.dll to some location you can reference on your computer. Then, fire up Visual Studio and create a new C# Console Application project named ScheduledTaskExporter. In your project, add a reference to the Microsoft.Win32.TaskScheduler.dll you downloaded earlier. Now add a couple of using statements to the top of your Program.cs:

using System;
using System.IO;

using Microsoft.Win32.TaskScheduler;

Next, we need to flesh out the Main() method. For the sake of brevity, we’ll keep error handling, validation, and logging to a minimum in this example. We want our ScheduledTaskExporter to take two positional command line arguments: the hostname of the server from which we want to export scheduled tasks, and the directory to which we want the exported job definitions written:

if (args.Length < 2)
{
    Console.WriteLine("Usage:");
    Console.WriteLine("\tScheduledTaskExporter hostname output_directory");
    Environment.Exit(1);
}

var hostname = args[0];
var outputDir = args[1];

Then, we’ll need to create an instance of a TaskService for the given hostname and get the root Scheduled Tasks folder, then call our ExportScheduledTasks() method to export the scheduled task definitions to our given output directory. Again, error handling and logging is minimal here, and this is still within our Main() method.

using (var taskService = new TaskService(hostname))
using (var taskFolder = taskService.GetFolder(@""))
{
    ExportScheduledTasks(taskFolder, outputDir);
}

Now let’s actually implement our ExportScheduledTasks() method. What we want to happen is for our tool to recursively walk the Task Scheduler folder structure and recreate that same folder structure within our given output directory, exporting scheduled task XML definitions along the way. We can do this recursively in just several lines:

private static void ExportScheduledTasks(TaskFolder taskFolder, string parentExportPath)
{
    var taskFolderPath = Path.Combine(parentExportPath, taskFolder.Name.TrimStart('\\'));

    foreach (var subFolder in taskFolder.SubFolders)
    {
        ExportScheduledTasks(subFolder, taskFolderPath);
    }

    Directory.CreateDirectory(taskFolderPath);

    foreach (var task in taskFolder.Tasks)
    {
        var taskFilePath = Path.Combine(taskFolderPath, task.Name + ".xml");
        File.WriteAllText(taskFilePath, task.Definition.XmlText);
        Console.WriteLine("Exported job definition \"{0}\"", taskFilePath);
    }
}

So as you can see here, we start by building the export path off our given output directory (we drop the leading backslash from the TaskFolder) and then recursively walk down the Task Scheduler folder tree until we reach a leaf folder node. Then we export each scheduled task’s XML definition in that folder to a matching directory structure in our output directory. Each task definition file retains the task name as well.

Now we have a simple utility we can use to bulk export scheduled tasks from any Windows machine we have access to, and we didn’t have to do any XML parsing or schtasks.exe output parsing.

Obviously there are plenty of other ways we can improve on this utility, but hopefully it gives you a taste of some of the things you can build to fill in the missing gaps with the Windows Task Scheduler using the Task Scheduler Managed Wrapper.

 
Comments

No comments yet.

Leave a Reply