Announcement

Collapse
No announcement yet.

xml Document - XML.SetValue on a Scheduled Task xml Document

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • xml Document - XML.SetValue on a Scheduled Task xml Document

    Hi all.
    I have been messing about with creating some Scheduled Tasks for my custom DISM folder structure.
    I have to edit a couple of values on several xml files but I am hitting a brick wall.

    I have got the hang on loading a xml file
    I have go the hang of changing a certain value on a simple xml document but when it comes to the Scheduled Task xml doc there a couple of extra lines which contain quotation marks

    simple xml
    Code:
    <database>
    <Author>Malcs</Author>
    <database>
    scheduled task xml
    Code:
    <?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
    <Date>2021-07-13T18:53:00.9579204</Date>
    <Author>Edit_1</Author>
    <Description>Mounts the install.wim Image</Description>
    <URI>\DISM\01Mountinstallwim</URI>
    </RegistrationInfo>

    on the simple xml file, using
    Code:
    XML.SetValue("database/Author", "Malcs");
    sets the value to what I want but for the life in me I can not get the first two lines of the scheduled task xml doc to fit the script to set the value.

    Anybody help me out here ?

    Hope this make sense

    Cheers.

  • #2
    The first line of the XML is the declaration, and you should ignore it. The rest of the XML you posted is malformed, as it is missing a closing "</Task>" tag.
    If you want to change a value in the XML, such as the contents of Author, this is the way:

    Code:
    local sXML = [[<?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
    <Date>2021-07-13T18:53:00.9579204</Date>
    <Author>Edit_1</Author>
    <Description>Mounts the install.wim Image</Description>
    <URI>\DISM\01Mountinstallwim</URI>
    </RegistrationInfo>
    </Task>]];
    
    XML.SetXML(sXML);
    error = Application.GetLastError();
    if (error ~= 0) then
       Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
    else
       XML.SetValue("Task/RegistrationInfo/Author", "John Doe");
       error = Application.GetLastError();
       if (error ~= 0) then
          Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
       else
          sXML = XML.GetXML();
          error = Application.GetLastError();
          if (error ~= 0) then
             Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
          else
             Dialog.Message("Edited XML", sXML);
          end
       end
    end
    Observe that in this sample code I load the XML from a string instead of a file. If the XML file is indeed UTF-16 encoded, then you may need to convert it from Unicode before the content can be loaded by AutoPlay Media Studio, perform the desired operation, and then convert it back to the target encoding. Use error checking as shown in my sample, so know if the file was loaded correctly, and check after each step.

    Ulrich

    Comment


    • #3
      Cheers for that Ulrich.
      I will give that a good looking at.
      Last night I did figure out how to load the xml into a string and do some simple string manipulation to reach my desired goal.
      Your code looks pretty interesting, will mess later once I have more time.

      Thanks for helping..

      Comment


      • #4
        Hi Ulrich.

        Just found a bit of time to go through your script and one thing struck me.

        I was trying to point to the "Author" I was trying to put all of the "Task" section in
        i.e
        Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"
        Where in fact all that was needed is the word
        Task
        So with this in mind my script looks like this
        Code:
        XML.Load("C:\\Users\\Malcs\\Desktop\\xml Document Manipulation\\DummyxmlDoc1.xml");
        
        XML.SetValue("Task/RegistrationInfo/Author", "Malcs");
        
        XML.Save("C:\\Users\\Malcs\\Desktop\\xml Document Manipulation\\DummyxmlDoc1+malcs.xml");
        Its simple but effective

        Going to work on the exact data I wish to edit and see how I go with that. I will post my script once done.

        Let me thankyou very much Ulrich, your script was great and helped me move forward.

        Comment


        • #5
          Got it !

          Loading the following xml document

          <?xml version="1.0" encoding="UTF-16"?>
          <Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
          <RegistrationInfo>
          <Date>2021-07-13T18:53:00.9579204</Date>
          <Author>Edit1</Author>
          <Description>Mounts the install.wim Image</Description>
          <URI>\DISM\01Mountinstallwim</URI>
          </RegistrationInfo>
          <Triggers />
          <Principals>
          <Principal id="Author">
          <UserId>Edit2</UserId>
          <LogonType>InteractiveToken</LogonType>
          <RunLevel>HighestAvailable</RunLevel>
          </Principal>
          </Principals>
          <Settings>
          <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
          <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
          <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
          <AllowHardTerminate>true</AllowHardTerminate>
          <StartWhenAvailable>false</StartWhenAvailable>
          <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
          <IdleSettings>
          <StopOnIdleEnd>true</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
          </IdleSettings>
          <AllowStartOnDemand>true</AllowStartOnDemand>
          <Enabled>true</Enabled>
          <Hidden>false</Hidden>
          <RunOnlyIfIdle>false</RunOnlyIfIdle>
          <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
          <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
          <WakeToRun>false</WakeToRun>
          <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
          <Priority>7</Priority>
          </Settings>
          <Actions Context="Author">
          <Exec>
          <Command>"C:\Windows\MyFiles\DISMScheduledTaskCr ea tor\Data\Docs\DISM Commands\01Mountinstallwim.cmd"</Command>
          </Exec>
          </Actions>
          </Task>
          My script

          Code:
          -- Get User Name and SID Number
          User = System.GetLANInfo ()
          UserHost = User.Host
          User = System.GetLANInfo()
          UserName = String.Concat(UserHost,"\\".. User.User);
          SIDNumber = System.GetUserSID(User.User);
          -- Load xml document
          XML.Load("C:\\Users\\Malcs\\Desktop\\xml Document Manipulation\\01Mountinstallwim.xml");
          -- Edits the xml document
          XML.SetValue("Task/RegistrationInfo/Author", UserName);
          XML.SetValue("Task/Principals/Principal/UserId", SIDNumber);
          -- Saves the xml document
          XML.Save("C:\\Users\\Malcs\\Desktop\\xml Document Manipulation\\Edited_xml_Doc\\01Mountinstallwim.xm l");
          This works a treat.
          Tested importing the edited xml document into "Scheduled Tasks" and it does so without any errors.

          Brilliant.

          Could not have done it without your help Ulrich. Thanks very much

          Just got to write the script for all 17 xml documents and I'll be done.

          P.S.

          To import my xml documents into Scheduled Tasks I use this command prompt script
          Code:
          CommandLine.Execute('schtasks.exe /create /f /tn "DISM\\01Mountinstallwim" /XML "C:\\DISM\\scheduledtasks\\amended\\01Mountinstallwim.xml"', 0);
          It does work, just wondering does AMS have a built in Scheduled Task editor, importer ?


          Comment


          • #6
            Wrote a reply, #5. It is coloured green. Not seen that happen before.
            Anybody elaborate on this ?

            Comment


            • #7
              This happens occasionally, when the forum engine blocks a post, waiting for admin approval. It is out of our control, and nothing to worry about. Your post is now public.

              Ulrich

              Comment

              Working...
              X