Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have all the tasks from a server in a XML file obtained using

schtasks /Query /XML ONE>C:\schedtasksBackup.xml

Now, I want to import all the tasks in another server. All the info explains how to import o create ONE task at a time, but not multiple tasks at the same time.

It's possible? Or do I need to split the file in multiple atomic tasks file?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I wrote this code to split the xml and import the tasks one by one:

var taskXML = new XmlDocument();
taskXML.Load(@"d:\temp\schedtasksBackup.xml");
var batbody = new StringBuilder();

XmlNodeList tasks = taskXML.DocumentElement.GetElementsByTagName("Task");

string strFileName = "d:\\temp\\Task";

for (int i = 0; i < tasks.Count; i++) {
    string onetaskXML = tasks[i].OuterXml;

    //Create the New File. With a little more extra effort
    //you can get the name from a comment above the task
    XmlWriter xw = XmlWriter.Create(strFileName + "_" + (i+1) + ".xml");
    batbody.AppendLine(string.Format("schtasks.exe /create /TN \"{0}\" /XML \"{1}\"", "Task " + (i+1) + " Name", strFileName + "_" + (i+1) + ".xml"));

    //Write the XML
    xw.WriteRaw(onetaskXML.ToString());
    xw.Close();

    // Write a bat to import all the tasks
    var batfile = new System.IO.StreamWriter("d:\\temp\\importAllTasks.bat");
    batfile.WriteLine(batbody.ToString());
    batfile.Close();

}

Edit: BUT, it wasn't so easy. I couldn't find why, but schtasks.exe gives malformed XML error importing the file. I have to import the files manually (hate it, but didn't have more time to spend on the issue)

share|improve this answer

I believe that if you want to use schtasks you need to make them atomic. I would recommend that you use the new-task cmdlet in powershell if you want to schedule multiple tasks (or just want more granular options) see PowerShellPack if you do not already have the resource kit installed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.