title: Task scheduler Windows Server 2019 - Task not running ONSTART
body: <p>I am trying to build an AWS AMI for a gitlab runner for building our .NET application.
  I am using packer for building the image based one the official Windows Server 2019 base AWS AMI.</p>
  <p>I am using WinRM, with HTTPS, not changing any password.</p>
  <p>Here are the powershell commands to configure the virtual machine :</p>
  <pre><code>&quot;Creating desktop directory&quot;
  mkdir C:\Windows\SysWOW64\config\systemprofile\Desktop
  
  
  &quot;Installing ntrights tools&quot;
  mkdir tools
  Invoke-WebRequest -Uri &quot;https://download.microsoft.com/download/8/e/c/8ec3a7d8-05b4-440a-a71e-ca3ee25fe057/rktools.exe&quot; -OutFile &quot;tools\tools.exe&quot; -UseBasicParsing
  
  Start-Process &quot;tools\tools.exe&quot; -ArgumentList &quot;/T:$pwd\tools\ /C&quot; -Wait
  Start-Process &quot;msiexec.exe&quot; -ArgumentList &quot;/i $pwd\tools\rktools.msi /qn&quot; -Wait
  
  $env:Path = [System.Environment]::GetEnvironmentVariable(&quot;Path&quot;,&quot;Machine&quot;) + &quot;;&quot; + [System.Environment]::GetEnvironmentVariable(&quot;Path&quot;,&quot;User&quot;)
  
  Remove-Item tools -Recurse
  
  &quot;Setting rights of service logon to $Env:WINRMUSER&quot;
  ntrights.exe ntrights +r SeServiceLogonRight -u $Env:WINRMUSER
  
  # Git lab runner
  $path = &quot;.\gitlab-runner.exe&quot;
  If(!(test-path $path)) 
  {
      &quot;Downloading Gitlab Runner&quot;
      Invoke-WebRequest -Uri &quot;https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-windows-386.exe&quot; -OutFile $path -UseBasicParsing
  }
  
  &quot;Scheduling runner to start at startup of the system&quot;
  schtasks.exe /create /tn &quot;Gitlab Runner service start&quot; /RU $Env:WINRMUSER /RP `&quot;$Env:WINRMPASS`&quot; /Sc ONSTART /tr &quot;powershell -Command $pwd\register-gitlabrunner.ps1 -ExecutionPolicy Bypass&quot;
  </code></pre>
  <p>There are obviously more scripts executed (install msbuild, install .net sdk 4.7.2, nugget, and git) I can provide them if relevant. Here I focus on the powershell code I came up with for the gitlab runner problem.</p>
  <p>I want the virtual machine to start the runner on start so we just have to launch instances of the AMI to scale up.</p>
  <p>To explain a bit more what I did try :</p>
  <ul>
  <li>You can see I am trying to create the desktop directory in order for windows to get that it can run interactive things... Not working</li>
  <li>I am setting up the SeServiceLogonRight in order to avoid the &quot;failed to logon&quot; error</li>
  <li>The user is Administrator, and the password is the right password</li>
  <li>The scheduled tasks is created and ready to run. Won't run on start, won't run If i start it through schtasks /Run (the last run time is never updated and show a value in 19XX)</li>
  <li>Tried to cmd /c the task command, everything work as expected</li>
  <li>I don't find any logs anywhere, event log seems to be empty of problem from Application, System and Powershell. The file in c:\Windows\Tasks\SchlogU (or something like that), does not exist (but the folder exists)</li>
  <li>I have no UI for the scheduler, I use a light version of windows so all I can do is play with the schtasks.exe</li>
  <li>Default folder is : c:\Users\Administrator</li>
  <li>The powershell script is pushed by packer onto the server and is located in c:\Users\Administrator (as for the gitlab-runner.exe)</li>
  </ul>
  <p>I connect directly through RDP to try debugging the situation.</p>
  <p>Here is the script that should be started</p>
  <pre><code>Set-Location $PSScriptRoot
  $path = &quot;.\gitlab-runner.exe&quot;
  
  &quot;Stopping runner&quot;
  Invoke-Expression &quot;$path stop&quot;
  
  &quot;Unregistering previous configuration&quot;
  Invoke-Expression &quot;$path unregister --all-runners&quot;
  
  &quot;Uninstalling runner&quot;
  Invoke-Expression &quot;$path uninstall&quot;
  
  &quot;Installing runner&quot;
  Invoke-Expression &quot;$path install&quot;
  
  &quot;Registering Gitlab Runner&quot;
  Invoke-Expression &quot;$path register --non-interactive --url 'https://URL_HERE/' --registration-token 'TOKEN HERE' --executor shell&quot;
  
  &quot;Starting the runner&quot;
  Invoke-Expression &quot;$path start&quot;
  </code></pre>
  <p>I can install the runner only once in the configuration using the user and password but this is not the problem here since the task never runs...</p>