1

By default Windows Scheduled Tasks are created with the 'Start the task only if the computer is on AC power' setting enabled.

Scheduled Task

It is not possible to change this setting using schtasks.exe, which would have been the simple solution. However, it appears that it is possible to do this through the Windows API ITaskSettings::get_DisallowStartIfOnBatteries method. Can this be imported into Inno Setup in order to disable this setting, on an existing Scheduled Task, and allow it to run when the computer is on battery power? If so, how would this be done? Or is there another way to change this setting using Inno Setup?

Robert Wigley
  • 1,917
  • 22
  • 42

1 Answers1

1

Actually, it's possible to set that option using schtasks. You just have to use XML definition of the task.

For your particular option, you need to set DisallowStartIfOnBatteries to false:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <!-- ... -->
  <Settings>
    <!-- ... -->
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
  </Settings>
</Task>

For more, see How to add a scheduled task on network connection/disconnection event with Inno Setup.


I didn't find a way to use the API to modify an existing task. This does not have any effect:

var
  TaskService: Variant;
  Folder: Variant;
  Task: Variant;
begin
  TaskService := CreateOleObject('Schedule.Service');
  TaskService.Connect();
  Folder := TaskService.GetFolder('\');
  Task := Folder.GetTask('test');
  Task.Definition.Settings.DisallowStartIfOnBatteries := False;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Maybe I should have been clearer in the question and said I was already aware of the XML option for `schtasks.exe`. The trouble with that is it is only really useful if you are creating a new Scheduled Task and not if you are trying to change an existing one. – Robert Wigley Feb 16 '19 at 17:03
  • This may work actually. To export the task to XML (assuming that can be done from the command line), delete the original task, edit the XML in code and recreate it. However, it seems quite a convoluted way to do it. – Robert Wigley Feb 16 '19 at 17:15
  • 1
    To export an XML of an existing task, use `schtasks /query /tn "name" /xml one`. – Martin Prikryl Feb 16 '19 at 18:00
  • Is there not another more elegant way to do this? It seems like an awfully convoluted way to achieve it. – Robert Wigley Feb 16 '19 at 18:03
  • I haven't found any elegant way to modify advanced settings of an existing scheduled task. – Martin Prikryl Feb 16 '19 at 18:23
  • I take it the Windows API method I mentioned in the question wouldn't work then? Unfortunately, I don't really understand how to go about importing Windows APIs and translating from the C++ they are documented in to PascalScript, – Robert Wigley Feb 16 '19 at 18:30
  • 1
    I didn't find a way to modify settings of an existing task. See my updated answer. – Martin Prikryl Feb 16 '19 at 18:37
  • Looks like the convoluted method may be the only way then. Thank you Martin. – Robert Wigley Feb 16 '19 at 18:41