Search code examples
xmlsedxmlstarlet

XMLstarlet and Windows Task manipulation


Trying to use xmlstarlet to modify an exported windows task

Sampletask:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2014-02-10T11:57:03</Date>
    <Author>client</Author>
  </RegistrationInfo>
  <Triggers>
    <LogonTrigger>
      <StartBoundary>2014-02-10T11:57:00</StartBoundary>
      <Enabled>true</Enabled>
    </LogonTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>DOMAIN\client</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>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P7D</ExecutionTimeLimit>
    <Priority>7</Priority>
    <RestartOnFailure>
      <Interval>PT1M</Interval>
      <Count>10</Count>
    </RestartOnFailure>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"script"</Command>
    </Exec>
  </Actions>
</Task>

I want to change <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> and <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> to "false".

using XMLStarlet on winodws version 1.5.0 And in my mind this should work:

xml ed -u "Task/Settings/DisallowStartIfOnBatteries" -v false sampletask.xml

But it gives me: None of the XPaths matched; to match a node in the default namespace use '' as the prefix (see section 5.1 in the manual). For instance, use /:node instead of /node

Looking to the Man under 5.1 but i still don't get any smarter :-( Tested

xml ed -u -N k=http://schemas.microsoft.com/windows/2004/02/mit/task" /k:Task/Settings/DisallowStartIfOnBatteries" -v false IneraLogon.xml

but that gives me

failed to load external entity "k=http://schemas.microsoft.com/windows/2004/02/mit/task"

Verifying and http://schemas.microsoft.com/windows/2004/02/mit/task looks to be down.

dose any one has a suggestion on how to move on?

Should i just go over and try using sed for windows instead?


Solution

  • Your xml file uses a default namespace that you have to declare and use in xpath expressions.

    Declare it with -N switch (the prefix name doesn't mind):

    -N n="http://schemas.microsoft.com/windows/2004/02/mit/task"
    

    and include it in all elements of the xpath expression:

    -u "n:Task/n:Settings/n:DisallowStartIfOnBatteries"
    

    It gets:

    xmlstarlet ed \
      -N n="http://schemas.microsoft.com/windows/2004/02/mit/task" \
      -u "n:Task/n:Settings/n:DisallowStartIfOnBatteries" \
      -v false \
    sampletask.xml