שתף באמצעות


Importing XML into Task Scheduler

Question

Tuesday, September 25, 2012 3:11 AM | 2 votes

I'm trying to import an XML into Task Scheduler using VB, so far I've gotten close (or at least I hope so) and have ended up with this as as my current output.

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>22/09/2012 2:01:35 AM</Date>
    <Author>Isaac</Author>
    <Version>1.0.0</Version>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <StartBoundary>2006-05-02T13:20:00</StartBoundary>
      <EndBoundary>2700-05-02T13:20:00</EndBoundary>
      <Subscription>
        <QueryList>
          <QueryId='1'>
            <Select Path='System'>*[System/EventID=203]</Select>
          </QueryId>
        </QueryList>
      </Subscription>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal>
      <UserId>Administrator</UserId>
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Settings>
    <Enabled>true</Enabled>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <AllowHardTerminate>true</AllowHardTerminate>
  </Settings>
  <Actions>
    <SendEmail>
      <Server>127.0.0.1</Server>
      <Subject>Task Failure</Subject>
      <To>random_email@unknown.com</To>
      <From>Client</From>
      <Body>Error 203 occured, SyncToy might have problems.</Body>
    </SendEmail>
  </Actions>
</Task>

currently, when I import this to Task Scheduler, I get the error

C:\tests>schtasks /create /xml email.xml /tn testemail2
ERROR: The task XML is malformed.
(1,2)::

What's wrong with my syntax?

I'm using this VB script to write the XML, though, I don't think that its very prevalent... Also, it shouldn't matter what values it gets as long as they're vaguely correct.

    Private Sub createNode(ByVal tdate As String, ByVal name As String, ByVal version As String, ByVal start As String, ByVal endbound As String, ByVal subt As String, _
                             ByVal user As String, ByVal logont As String, ByVal enabled As String, ByVal sod As String, ByVal aht As String, ByVal server As String, _
                             ByVal subject As String, ByVal sendto As String, ByVal sentfrom As String, ByVal body As String, ByVal writer As XmlTextWriter)

        writer.WriteStartElement("RegistrationInfo")

            writer.WriteStartElement("Date")
                writer.WriteString(tdate)
            writer.WriteEndElement()

            writer.WriteStartElement("Author")
                writer.WriteString(name)
            writer.WriteEndElement()

            writer.WriteStartElement("Version")
                writer.WriteString(version)
            writer.WriteEndElement()

        writer.WriteEndElement()

        writer.WriteStartElement("Triggers")

            writer.WriteStartElement("EventTrigger")

                writer.WriteStartElement("StartBoundary")
                    writer.WriteString(start)
                writer.WriteEndElement()

                writer.WriteStartElement("EndBoundary")
                    writer.WriteString(endbound)
                writer.WriteEndElement()

                writer.WriteStartElement("Subscription")
                    writer.WriteStartElement("QueryList")
                        writer.WriteStartElement("QueryId='1'")

                            writer.WriteStartElement("Select Path='System'")
                                writer.WriteString(subt)
                            writer.WriteEndElement()

                        writer.WriteEndElement()
                    writer.WriteEndElement()
                writer.WriteEndElement()
            writer.WriteEndElement()
        writer.WriteEndElement()

        writer.WriteStartElement("Principals")
            writer.WriteStartElement("Principal")

                writer.WriteStartElement("UserId")
                    writer.WriteString(user)
                writer.WriteEndElement()

                writer.WriteStartElement("LogonType")
                    writer.WriteString(logont)
                writer.WriteEndElement()

            writer.WriteEndElement()
        writer.WriteEndElement()

        writer.WriteStartElement("Settings")

            writer.WriteStartElement("Enabled")
                writer.WriteString(enabled)
            writer.WriteEndElement()

            writer.WriteStartElement("AllowStartOnDemand")
                writer.WriteString(sod)
            writer.WriteEndElement()

            writer.WriteStartElement("AllowHardTerminate")
                writer.WriteString(aht)
            writer.WriteEndElement()

        writer.WriteEndElement()

        writer.WriteStartElement("Actions")

            writer.WriteStartElement("SendEmail")
                writer.WriteStartElement("Server")
                    writer.WriteString(server)
                writer.WriteEndElement()

                writer.WriteStartElement("Subject")
                    writer.WriteString(subject)
                writer.WriteEndElement()

                writer.WriteStartElement("To")
                    writer.WriteString(sendto)
                writer.WriteEndElement()

                writer.WriteStartElement("From")
                    writer.WriteString(sentfrom)
                writer.WriteEndElement()

                writer.WriteStartElement("Body")
                    writer.WriteString(body)
                writer.WriteEndElement()
            writer.WriteEndElement()
        writer.WriteEndElement()
    End Sub

All replies (19)

Saturday, September 29, 2012 4:53 PM ✅Answered

Although the following link is C Sharp you might consider compiling the library as a DLL then using the sample code provided in the article create a task. The code to create a task should be easy to convert to VB.NET.

http://www.codeproject.com/Articles/2407/A-New-Task-Scheduler-Class-Library-for-NET

Going back to your XML, I set up the xml as literal xml, parsed it with XDocument then used LINQ to obtain some information. I would suspect that since my test worked there must be something that is expected by Task Scheduler that you are not providing.

Dim Content = _    <?xml version="1.0" encoding="utf-16"?>    <Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">        <RegistrationInfo>            <Date>2012-09-22T02:01:35</Date>            <Author>Isaac</Author>            <Version>1.0.0</Version>        </RegistrationInfo>        <Triggers>            <EventTrigger>                <StartBoundary>2006-05-02T13:20:00</StartBoundary>                <EndBoundary>2700-05-02T13:20:00</EndBoundary>                <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>                <Subscription>                    <QueryList>                        <Query Id='1'>                            <Select Path='System'>*[System/EventID=203]</Select>                        </Query>                    </QueryList>                </Subscription>            </EventTrigger>        </Triggers>        <Principals>            <Principal>                <UserId>Administrator</UserId>                <LogonType>InteractiveToken</LogonType>            </Principal>        </Principals>        <Settings>            <Enabled>true</Enabled>            <AllowStartOnDemand>true</AllowStartOnDemand>            <AllowHardTerminate>true</AllowHardTerminate>        </Settings>        <Actions>            <SendEmail>                <Server>127.0.0.1</Server>                <Subject>Task Failure</Subject>                <To>randomemail@unknown.com</To>                <From>Client</From>                <Body>Error 203 occured, SyncToy might have problems.</Body>            </SendEmail>        </Actions>    </Task>Dim Doc = New XDocumentDoc = XDocument.Parse(Content.ToString)Dim RegInfo =    (        From x In Doc...<RegistrationInfo>        Select        Author = x.<Author>.Value,        [Date] = x.<Date>.Value,        Version = x.<Version>.Value    ).FirstOrDefaultIf RegInfo IsNot Nothing Then    Console.WriteLine("Registration info")    Console.WriteLine("[{0}] [{1}] [{2}]",                      RegInfo.Author,                      RegInfo.Date,                      RegInfo.Version                      )    Try        Console.WriteLine("   [{0}]", CDate(RegInfo.Date))    Catch ex As Exception        Console.WriteLine(ex.Message)    End TryEnd IfDim ActionInfo = _    (        From x In Doc...<SendEmail>        Select        Server = x.<Server>.Value,        Subject = x.<Subject>.Value,        [To] = x.<To>.Value,        [From] = x.<From>.Value,        Body = x.<Body>.Value    ).FirstOrDefaultIf ActionInfo IsNot Nothing Then    Console.WriteLine("Action Info")    Console.WriteLine("[{0}] [{1}] [{2}] [{3}] [{4}]",                      ActionInfo.Server,                      ActionInfo.Subject,                      ActionInfo.To,                      ActionInfo.From,                      ActionInfo.Body)End If

Output from above

Registration info[Isaac] [2012-09-22T02:01:35] [1.0.0]   [9/22/2012 2:01:35 AM]Action Info[127.0.0.1] [Task Failure] [randomemail@unknown.com] [Client] [Error 203 occured, SyncToy might have problems.]

Other thoughts are reviewing the schema

KSG


Tuesday, September 25, 2012 11:07 AM

ZACK1040,

You have posted this to the Small Basic forum.  It looks like you need to repost it to the Visual Basic forum.

JR


Tuesday, September 25, 2012 6:23 PM

I'm trying to import an XML into Task Scheduler using VB, so far I've gotten close (or at least I hope so) and have ended up with this as as my current output.

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>22/09/2012 2:01:35 AM</Date>
    <Author>Isaac</Author>
    <Version>1.0.0</Version>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <StartBoundary>2006-05-02T13:20:00</StartBoundary>
      <EndBoundary>2700-05-02T13:20:00</EndBoundary>
      <Subscription>
        <QueryList>
          <QueryId='1'>
            <Select Path='System'>*[System/EventID=203]</Select>
          </QueryId>
        </QueryList>
      </Subscription>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal>
      <UserId>Administrator</UserId>
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Settings>
    <Enabled>true</Enabled>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <AllowHardTerminate>true</AllowHardTerminate>
  </Settings>
  <Actions>
    <SendEmail>
      <Server>127.0.0.1</Server>
      <Subject>Task Failure</Subject>
      <To>random_email@unknown.com</To>
      <From>Client</From>
      <Body>Error 203 occured, SyncToy might have problems.</Body>
    </SendEmail>
  </Actions>
</Task>

currently, when I import this to Task Scheduler, I get the error

C:\tests>schtasks /create /xml email.xml /tn testemail2
ERROR: The task XML is malformed.
(1,2)::

What's wrong with my syntax?

I'm using this VB script to write the XML, though, I don't think that its very prevalent... Also, it shouldn't matter what values it gets as long as they're vaguely correct.

    Private Sub createNode(ByVal tdate As String, ByVal name As String, ByVal version As String, ByVal start As String, ByVal endbound As String, ByVal subt As String, _
                             ByVal user As String, ByVal logont As String, ByVal enabled As String, ByVal sod As String, ByVal aht As String, ByVal server As String, _
                             ByVal subject As String, ByVal sendto As String, ByVal sentfrom As String, ByVal body As String, ByVal writer As XmlTextWriter)

        writer.WriteStartElement("RegistrationInfo")

            writer.WriteStartElement("Date")
                writer.WriteString(tdate)
            writer.WriteEndElement()

            writer.WriteStartElement("Author")
                writer.WriteString(name)
            writer.WriteEndElement()

            writer.WriteStartElement("Version")
                writer.WriteString(version)
            writer.WriteEndElement()

        writer.WriteEndElement()

        writer.WriteStartElement("Triggers")

            writer.WriteStartElement("EventTrigger")

                writer.WriteStartElement("StartBoundary")
                    writer.WriteString(start)
                writer.WriteEndElement()

                writer.WriteStartElement("EndBoundary")
                    writer.WriteString(endbound)
                writer.WriteEndElement()

                writer.WriteStartElement("Subscription")
                    writer.WriteStartElement("QueryList")
                        writer.WriteStartElement("QueryId='1'")

                            writer.WriteStartElement("Select Path='System'")
                                writer.WriteString(subt)
                            writer.WriteEndElement()

                        writer.WriteEndElement()
                    writer.WriteEndElement()
                writer.WriteEndElement()
            writer.WriteEndElement()
        writer.WriteEndElement()

        writer.WriteStartElement("Principals")
            writer.WriteStartElement("Principal")

                writer.WriteStartElement("UserId")
                    writer.WriteString(user)
                writer.WriteEndElement()

                writer.WriteStartElement("LogonType")
                    writer.WriteString(logont)
                writer.WriteEndElement()

            writer.WriteEndElement()
        writer.WriteEndElement()

        writer.WriteStartElement("Settings")

            writer.WriteStartElement("Enabled")
                writer.WriteString(enabled)
            writer.WriteEndElement()

            writer.WriteStartElement("AllowStartOnDemand")
                writer.WriteString(sod)
            writer.WriteEndElement()

            writer.WriteStartElement("AllowHardTerminate")
                writer.WriteString(aht)
            writer.WriteEndElement()

        writer.WriteEndElement()

        writer.WriteStartElement("Actions")

            writer.WriteStartElement("SendEmail")
                writer.WriteStartElement("Server")
                    writer.WriteString(server)
                writer.WriteEndElement()

                writer.WriteStartElement("Subject")
                    writer.WriteString(subject)
                writer.WriteEndElement()

                writer.WriteStartElement("To")
                    writer.WriteString(sendto)
                writer.WriteEndElement()

                writer.WriteStartElement("From")
                    writer.WriteString(sentfrom)
                writer.WriteEndElement()

                writer.WriteStartElement("Body")
                    writer.WriteString(body)
                writer.WriteEndElement()
            writer.WriteEndElement()
        writer.WriteEndElement()
    End Sub

Running Windows 7 Pro


Wednesday, September 26, 2012 8:54 AM

You should correct the following issues:

Correct the declaration of your XML.

<?xml version="1.0" encoding="utf-16"?>

Correct the date format  in RegistrationInfo like as:

    <Date>2012-09-22T02:01:35</Date>

And also check the Event's 'Subscription'...

Thanks

Hirendra Sisodiya from authorcode.com


Thursday, September 27, 2012 12:34 AM

I don`t really know what I'm doing with this whole thing, I basically copy and pasted and tried to get the output to be what I needed. I've been over This page quite a few times, looking at things like "Subscription" but I can't actually find anything on that site pertaining to what I need. 

I'm also confused about the output regarding the numbers, what does the (1,2) mean?

ERROR: The task XML is malformed.
(1,2)::


Thursday, September 27, 2012 3:50 AM

XML syntax wise the following is wrong regarding QueryId

<QueryList>    <QueryId='1'>        <Select Path='System'>*[System/EventID=203]</Select>    </Query></QueryList>

Syntax should be (thinking ID is an attruibute)

<QueryList>    <Query Id='1'>        <Select Path='System'>*[System/EventID=203]</Select>    </Query></QueryList>

If you are using VS2010 you could add the following code into a code module or form. Note afterwards the coloring is off after QueryId='1'

Public Sub Original()    Dim Result = _        <?xml version="1.0" encoding="utf-8" standalone="yes"?>        <Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">            <RegistrationInfo>                <Date>22/09/2012 2:01:35 AM</Date>                <Author>Isaac</Author>                <Version>1.0.0</Version>            </RegistrationInfo>            <Triggers>                <EventTrigger>                    <StartBoundary>2006-05-02T13:20:00</StartBoundary>                    <EndBoundary>2700-05-02T13:20:00</EndBoundary>                    <Subscription>                        <QueryList>                            <QueryId='1'>                                <Select Path='System'>*[System/EventID=203]</Select>                            </Query>                        </QueryList>                    </Subscription>                </EventTrigger>            </Triggers>            <Principals>                <Principal>                    <UserId>Administrator</UserId>                    <LogonType>InteractiveToken</LogonType>                </Principal>            </Principals>            <Settings>                <Enabled>true</Enabled>                <AllowStartOnDemand>true</AllowStartOnDemand>                <AllowHardTerminate>true</AllowHardTerminate>            </Settings>            <Actions>                <SendEmail>                    <Server>127.0.0.1</Server>                    <Subject>Task Failure</Subject>                    <To>random_email@unknown.com</To>                    <From>Client</From>                    <Body>Error 203 occured, SyncToy might have problems.</Body>                </SendEmail>            </Actions>        </Task>End Sub

KSG


Thursday, September 27, 2012 6:53 AM

(1,2) refers the the error's location.

simply you have an error in second position in first line...

Can you describe me what you want to with task scheduler.? If you want to invoke your own executable at a particular time then you don't need to <EventTrigger>, you can use the <calendarTrigger> like as:

<CalendarTrigger> 
<StartBoundary>2012-09-15T11:16:00</StartBoundary> 
<Enabled>true</Enabled> -<ScheduleByDay> 
<DaysInterval>1</DaysInterval> </ScheduleByDay> 
</CalendarTrigger>

Thanks

Hirendra Sisodiya from authorcode.com


Friday, September 28, 2012 4:25 AM

I want to use this to send an email every time another task (or group of tasks) fails. This is previously set up already, and I believe the failed task/tasks logs a 203 error in the event list. I want this task to look for it and send an email whenever it happens.


Saturday, September 29, 2012 2:19 AM

Also, Hirendra Sisodiya, I edited my xml to look like this

<RegistrationInfo>
    <Date>2012-09-22T02:01:35</Date>
    <Author>Isaac Argueta And Nerds On Site</Author>
    <Version>1.0.0</Version>
  </RegistrationInfo>

and I still get the same malformed xml.


Saturday, September 29, 2012 3:10 AM

Also, Hirendra Sisodiya, I edited my xml to look like this

<RegistrationInfo>    <Date>2012-09-22T02:01:35</Date>    <Author>Isaac Argueta And Nerds On Site</Author>    <Version>1.0.0</Version>  </RegistrationInfo>

and I still get the same malformed xml.

Did you fix the malformed elements I pointed out?

KSG


Saturday, September 29, 2012 8:52 AM

send me your xml file..

Thanks

Hirendra Sisodiya from authorcode.com


Saturday, September 29, 2012 2:31 PM

Kevininstructor, I did in fact change my xml, however I still get the error code that its malformed.

Hirendra, here is my code that I'm trying to import using this cmd function

schtasks /create /xml email.xml /tn "this task"

<?xml version="1.0" encoding="utf-16"?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2012-09-22T02:01:35</Date>
    <Author>Isaac</Author>
    <Version>1.0.0</Version>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <StartBoundary>2006-05-02T13:20:00</StartBoundary>
      <EndBoundary>2700-05-02T13:20:00</EndBoundary>
      <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
            <Subscription>
                <QueryList>
                    <Query Id='1'>
                        <Select Path='System'>*[System/EventID=203]</Select>
                    </Query>
                </QueryList>
            </Subscription>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal>
      <UserId>Administrator</UserId>
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Settings>
    <Enabled>true</Enabled>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <AllowHardTerminate>true</AllowHardTerminate>
  </Settings>
  <Actions>
    <SendEmail>
      <Server>127.0.0.1</Server>
      <Subject>Task Failure</Subject>
      <To>randomemail@unknown.com</To>
      <From>Client</From>
      <Body>Error 203 occured, SyncToy might have problems.</Body>
    </SendEmail>
  </Actions>
</Task>

Monday, November 26, 2012 3:11 PM

Hi Zack

Reading through your thread, despite the tick applied by Mike, I can't see any clear answer has been provided.

If you have already solved your problem then don't worry about this but I thought you would like to know I'm getting the same error message if I try to create a task by importing an XML generated by exporting an existing valid task.  I'm deleting the old task before importing the script.

I think this means that the problem lies with The Task Scheduler rather than your XML.

Kind regards Ian Galletly


Friday, January 4, 2013 10:51 PM | 1 vote

I came across this thread because I had the same problem: creating my own XML (I used an XML Export from Task Manager as a template) and was getting the error 

ERROR: The task XML is malformed.
(1,2)::

I logged in to update that I found my problem: apparently the XML file HAS to be written in Unicode! I was writing my file from powershell (I realize this is a VB forum), and so my fix was:

Set-Content -Value $xmlString -Path $xmlFilePath -Encoding Unicode

Hopefully that helps someone else out there on the intertubes. :)


Friday, January 31, 2014 12:36 AM | 1 vote

I had this same problem today and my issue was I had <?xml version="1.0" encoding="UTF-16"?> as the first line of the file, but the file encoding was not UTF-16.  I used Notepad++ to convert the encoding to UCS-2 Little Endian and it works fine now.

Note: I had been using UTF-8 in the first line of the file with the file encoded as UTF-8 and that didn't work either using a command line install like below.  

 schtasks.exe /create /s localhost /ru System /tn RunMyApp /xml "RunRunMyApp.xml" /F

Funny thing is that it *did* work as UTF-8 by Importing the task via the Task Scheduler UI menu option.

David Nordstedt


Thursday, February 26, 2015 10:11 PM | 2 votes

Even though the Task Scheduler exports a task file as UTF-16 encoded, it refuses to read it unless it's UTF-8. Use a text editor like TextPad or Notepad++ to save the XML as a UTF-8 encoded file, and the Task Scheduler will correctly import it (even if the main XML tag shows encoding="UTF-16").


Wednesday, May 13, 2015 9:44 AM

Even though the Task Scheduler exports a task file as UTF-16 encoded, it refuses to read it unless it's UTF-8. Use a text editor like TextPad or Notepad++ to save the XML as a UTF-8 encoded file, and the Task Scheduler will correctly import it (even if the main XML tag shows encoding="UTF-16").

This is what worked for me.  In fact, I went for consistency and made the XML tag say encoding="UTF-8", and SchTasks didn't like it with the error "Unable to switch the encoding"!

Changing the tag back to "UTF-16", while keeping the actual encoding as UTF-8, seemed to satisfy it.


Tuesday, January 15, 2019 5:10 PM

Yes, these encodings work with schtasks import xml files (encodings as Notepad names them):

**Unicode **
Ansi

and these do not:

**Unicode big endian **
UTF-8

If I edit an xml import file with Emacs for Wndows, it unknowingly converts Unicode to Unicode big endian.  I'm currently in Windows 10 1709.


Tuesday, January 15, 2019 5:40 PM

Hi

Hmmmmm ........... proposing your own post as the answer for a years old thread won't get you many more points.

Regards Les, Livingston, Scotland