1

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?

2 Answers 2

0

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.

0

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)

1
  • This answer does not explicitly say whether /XML ONE or /XML was used to generate the XML file. This might be related to the "malformed XML error".
    – daveloyall
    Dec 5, 2019 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .