title: TaskScheduler RegisterTaskDefinition fails with (38:4)UserID
body: <p>I am using the Task Scheduler 2.0 API to create a scheduled task.</p>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa383624(v=vs.85).aspx" rel="nofollow noreferrer">Following the MSDN code example (for creating a time-triggered task)</a>, i call the COM objects myself: </p>
  
  <pre><code>ITaskService taskService = new TaskScheduler();
  taskService.Connect("", "", "", "");
  
  ITaskDefinition task = taskService.NewTask(0); //flags reserved and must be zero
  task.RegistrationInfo.Author = "Me";
  task.Principal.LogonType := TASK_LOGON_INTERACTIVE_TOKEN;
  
  ITimeTrigger = task.Triggers.Create(TASK_TRIGGER_TIME) as ITimeTrigger;
  trigger.Id = "Trigger1";
  trigger.StartBoundary = "2017-03-14T16:30:00";
  
  IExecAction action = task.Actions.Create(TASK_ACTION_EXEC) as IExecAction;
  action.Path = "Notepad.exe";
  
  ITaskFolder folder = taskService.GetFolder("\");
  
  folder.RegisterTaskDefinition("MyTaskName", task, TASK_CREATE_OR_UPDATE,
        Null, Null, TASK_LOGON_INTERACTIVE_TOKEN, "");
  </code></pre>
  
  <blockquote>
    <p><strong>Note:</strong> The above code was transcoded from Delphi; so as to not scare people away with a syntax they're not used to.</p>
  </blockquote>
  
  <p>In Task Scheduler 2.0 all tasks are really just Xml. You can view the xml of the task definition just before registering it:</p>
  
  <blockquote>
    <p><code>task.XmlText;</code></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-16"?&gt;
  &lt;Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"&gt;
    &lt;RegistrationInfo&gt;
      &lt;Author&gt;Me&lt;/Author&gt;
    &lt;/RegistrationInfo&gt;
    &lt;Triggers&gt;
      &lt;TimeTrigger id="Trigger1"&gt;
        &lt;StartBoundary&gt;2017-03-14T16:30:00&lt;/StartBoundary&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
      &lt;/TimeTrigger&gt;
    &lt;/Triggers&gt;
    &lt;Settings&gt;
      &lt;MultipleInstancesPolicy&gt;IgnoreNew&lt;/MultipleInstancesPolicy&gt;
      &lt;DisallowStartIfOnBatteries&gt;true&lt;/DisallowStartIfOnBatteries&gt;
      &lt;StopIfGoingOnBatteries&gt;true&lt;/StopIfGoingOnBatteries&gt;
      &lt;AllowHardTerminate&gt;true&lt;/AllowHardTerminate&gt;
      &lt;StartWhenAvailable&gt;false&lt;/StartWhenAvailable&gt;
      &lt;RunOnlyIfNetworkAvailable&gt;false&lt;/RunOnlyIfNetworkAvailable&gt;
      &lt;IdleSettings&gt;
        &lt;Duration&gt;PT10M&lt;/Duration&gt;
        &lt;WaitTimeout&gt;PT1H&lt;/WaitTimeout&gt;
        &lt;StopOnIdleEnd&gt;true&lt;/StopOnIdleEnd&gt;
        &lt;RestartOnIdle&gt;false&lt;/RestartOnIdle&gt;
      &lt;/IdleSettings&gt;
      &lt;AllowStartOnDemand&gt;true&lt;/AllowStartOnDemand&gt;
      &lt;Enabled&gt;true&lt;/Enabled&gt;
      &lt;Hidden&gt;false&lt;/Hidden&gt;
      &lt;RunOnlyIfIdle&gt;false&lt;/RunOnlyIfIdle&gt;
      &lt;WakeToRun&gt;false&lt;/WakeToRun&gt;
      &lt;ExecutionTimeLimit&gt;PT72H&lt;/ExecutionTimeLimit&gt;
      &lt;Priority&gt;7&lt;/Priority&gt;
    &lt;/Settings&gt;
    &lt;Actions&gt;
      &lt;Exec&gt;
        &lt;Command&gt;C:\Windows\System32\Notepad.exe&lt;/Command&gt;
      &lt;/Exec&gt;
    &lt;/Actions&gt;
  &lt;/Task&gt;
  </code></pre>
  
  <p>Attempting to register that Task XML throws the COM exception:</p>
  
  <blockquote>
    <p>(38,4):UserId</p>
  </blockquote>
  
  <h1>Credentials</h1>
  
  <p>The MSDN example is very specific in their use of </p>
  
  <ul>
  <li><strong>LogonType</strong>: <code>TASK_LOGON_INTERACTIVE_TOKEN</code></li>
  <li><strong>UserId</strong>: <em>none</em></li>
  <li><strong>Password</strong>: <em>none</em></li>
  <li><strong>SDDL</strong> <em>(security descriptor list)</em>: <code>""</code></li>
  </ul>
  
  <h2>Question</h2>
  
  <p>How do you register a scheduled task with the Task Scheduler 2.0 COM object API?</p>
  
  <h2>Look at the answer in the back of the book and work forward</h2>
  
  <p>No code example anywhere deals with <code>UserId</code>. But if i manually create a task in the task scheduler, and export it to XML:</p>
  
  <p><a href="https://i.stack.imgur.com/VV9RY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VV9RY.png" alt="enter image description here"></a></p>
  
  <p>I <em>can</em> find a <code>UserId</code> element inside a <code>Principles</code> element:</p>
  
  <pre><code>&lt;Principals&gt;
    &lt;Principal id="Author"&gt;
      &lt;UserId&gt;S-1-5-21-1708537768-854245398-2146844275-1109&lt;/UserId&gt;
      &lt;LogonType&gt;InteractiveToken&lt;/LogonType&gt;
      &lt;RunLevel&gt;LeastPrivilege&lt;/RunLevel&gt;
    &lt;/Principal&gt;
  &lt;/Principals&gt;
  </code></pre>
  
  <p>Even though my code (and the MSDN example) both call:</p>
  
  <p><strong>MSDN</strong>:</p>
  
  <pre><code>//  Set up principal logon type to interactive logon
  pTask-&gt;get_Principal( &amp;pPrincipal );
  pPrincipal-&gt;put_LogonType( TASK_LOGON_INTERACTIVE_TOKEN );
  </code></pre>
  
  <p><strong>Me</strong>:</p>
  
  <pre><code>task.Principal.LogonType := TASK_LOGON_INTERACTIVE_TOKEN;
  </code></pre>
  
  <p>Mine doesn't work. What gives?</p>
  
  <p>And even if i do have to figure out a user's Sid:</p>
  
  <ul>
  <li>why does the MSDN sample not have to?</li>
  <li>why does <a href="http://bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista-and-windows-server-2008-from-managed-code.aspx" rel="nofollow noreferrer">this guy not have to</a></li>
  </ul>
  
  <p>What gives?</p>
  
  <h2>Another MSDN sample</h2>
  
  <p>The canonical MSDN Example only sets:</p>
  
  <pre><code>task.Principal.LogonType := TASK_LOGON_INTERACTIVE_TOKEN;
  </code></pre>
  
  <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa382113(v=vs.85).aspx" rel="nofollow noreferrer">Another example on MSDN</a> sets:</p>
  
  <pre><code>task.Principal.Id := 'Principle1';
  task.Principal.LogonType := TASK_LOGON_INTERACTIVE_TOKEN;
  task.Principal.RunLevel := TASK_RUNLEVEL_LUA;
  </code></pre>
  
  <p>Which then adds <em>Principle</em> related element to the final XML:</p>
  
  <pre><code>&lt;Principals&gt;
    &lt;Principal id="Principle1"&gt;
      &lt;RunLevel&gt;LeastPrivilege&lt;/RunLevel&gt;
    &lt;/Principal&gt;
  &lt;/Principals&gt;
  </code></pre>
  
  <p>But still fails:</p>
  
  <pre><code>(15,8):UserId
  </code></pre>
  
  <p>for my new Xml:</p>
  
  <pre><code> 1: &lt;?xml version="1.0" encoding="UTF-16"?&gt;
   2: &lt;Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"&gt;
        ...
  12:   &lt;Principals&gt;
  13:     &lt;Principal id="Principle1"&gt;
  14:       &lt;RunLevel&gt;LeastPrivilege&lt;/RunLevel&gt;
  15:     &lt;/Principal&gt;
  16:   &lt;/Principals&gt;
        ...
  43: &lt;/Task&gt;
  </code></pre>
  
  <h2>Bonus Reading</h2>
  
  <ul>
  <li>Example code on <a href="https://serverfault.com/a/282060/4822">ServerFault</a></li>
  <li><a href="http://bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista-and-windows-server-2008-from-managed-code.aspx" rel="nofollow noreferrer">Example article</a></li>
  </ul>