Using schtasks from the command line, what paramet

2019-03-25 17:01发布

问题:

The option exists in the UI, but not in the help displayed in the command line.

回答1:

Are you creating a new task via the schtasks.exe command line, or updating an existing task?

On Vista, schtasks.exe has an /xml option for both /create and /query. With this XML encoding of the task, you can see the WakeToRun node can be set for waking the computer from sleep to run the task:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    ...
  </RegistrationInfo>
  <Triggers />
  <Principals>
    ...
  </Principals>
  <Settings>
    ...
    <WakeToRun>true</WakeToRun>
    ...
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>myprogram.exe</Command>
    </Exec>
  </Actions>
</Task>

If you need to create a task from the command line that wakes the computer, you could export the basics of the task to XML, modify this XML to add WakeToRun, then re-import this XML definition. You can do this two ways:

  1. In the Task Scheduler UI, select "Wake the computer to run this task", right-click on the task and Export... to XML. You can then re-import this file on another machine (see below), and Wake-To-Run will be set. or,

  2. Via the command line, create a task with the basics set (action, time, etc). Then, export the XML, programatically add the WakeToRun node (via XSLT or search/replace), then re-import this updated XML:

    schtasks.exe /create /tn /xml MyTask.xml /f



回答2:

In step 2 the command line; schtasks.exe /create /tn /xml MyTask.xml /f This may kick an error that says; Invalid syntax. Mandatory option 'tn' is missing.

/tn needs a name. This should be

schtasks.exe /create /tn MyTask /xml "C:\MyTask.xml" /f

And if you have or want a space in the name, you can use;

schtasks.exe /create /tn "My Task With Spaces" /xml "C:\My Task With Spaces.xml" /f

Hope this helps...