dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.2k stars 191 forks source link

While creating a new task the LogonType will not set. #954

Closed ghost closed 1 year ago

ghost commented 1 year ago

TaskScheduler (2.10.1), installed via NuGet Visual Studio 2022 17.4.0 Community Windows 10 22H2 19045.2006

I'm trying to create a new task from a XML file with this code:

TaskService.Instance.RootFolder.RegisterTask(
    "Example",
    File.ReadAllText("Example.xml"));

The XML is this:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2022-01-01T00:00:00</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <LogonType>InteractiveToken</LogonType>
    </Principal>
  </Principals>
  <Actions Context="Author">
    <Exec>
      <Command>msg</Command>
      <Arguments>Me "Hello World"</Arguments>
    </Exec>
  </Actions>
</Task>

Unfortunately, the LogonType will not be applied. But if I import this XML file from the %windir%\system32\taskschd.msc GUI, it will be.

Is this a bug of your project or a problem of the Windows API / Windows Task Scheduler?

dahall commented 1 year ago

Both the UI and my library use the COM interfaces exposed by Windows. What you're experiencing is a nuance of that base library. It requires you to specify the LogonType during registration and will not pick it up from the XML. Use the extended parameters in the RegisterTask method to specify all account related information. E.g.

TaskService.Instance.RootFolder.RegisterTask("Example", File.ReadAllText("Example.xml"),
   TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken, null);