Score:0

Windows scheduled task - task with cmd script runs, but .NET console app doesn't

co flag

I'm creating Windows scheduled tasks in a Docker container with Powershell. As a test, I've got a simple batch file which writes the current date/time to a text file once an hour. This works perfectly:

RUN $action = New-ScheduledTaskAction -Execute 'C:\folder\task-tester.cmd'; \
    $trigger = New-ScheduledTaskTrigger -Daily -At 12am; \
    $settings = New-ScheduledTaskSettingsSet;  \
    $task = Register-ScheduledTask -TaskName 'Write to text file' -User 'SYSTEM' -Trigger $trigger -Action $action -Settings $settings; \
    $task.Triggers.Repetition.Duration = 'P1D'; \
    $task.Triggers.Repetition.Interval = 'PT1H'; \
    $task | Set-ScheduledTask

When I try to execute a .NET console app, it doesn't work.

RUN $action = New-ScheduledTaskAction -Execute 'C:\folder\thingy.exe' -WorkingDirectory 'C:\folder'; \
    $trigger = New-ScheduledTaskTrigger -Daily -At 10am; \
    $settings = New-ScheduledTaskSettingsSet;  \
    Register-ScheduledTask -TaskName 'Task name' -User 'SYSTEM' -Trigger $trigger -Action $action -Settings $settings

Specifically, the task appears to run - the "last run" date/time is correct and the result is 0 (zero), but the console app itself does not get executed.

In the above command, I'm setting the working directory (a common pitfall with .NET console apps).

If I SSH into the container, I can execute "thing.exe" and it works as expected. If I manually execute the scheduled task via PowerShell Start-ScheduleTask, again, nothing seems to happen.

I've tried querying the event log with PowerShell, but can't see anything immediately useful.

A variation of the above command with more explicit options like compatibility and run level produces the same result - i.e. the task doesn't run:

RUN $action = New-ScheduledTaskAction -Execute 'C:\folder\thingy.exe' -WorkingDirectory 'C:\folder' ; \
    $trigger = New-ScheduledTaskTrigger -Daily -At 10am; \
    $settings = New-ScheduledTaskSettingsSet -Compatibility Win8;  \
    $principal = New-ScheduledTaskPrincipal -UserID 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount -RunLevel Highest; \
    Register-ScheduledTask -TaskName 'Task name' -Trigger $trigger -Action $action -Settings $settings -Principal $principal

Has anyone encountered this before, and solved it? Or can anyone help me get more diagnostic information from the container with PowerShell?

Thanks!

Score:0
co flag

For anyone who may encounter this in the future:

I have more than one process running in the container (I know, this is not ideal, but in this case unavoidable), and the problem in my case was environment variables not being available to the processes invoked by the scheduled tasks.

When environment variables are created as part of a Docker build, any env vars created are only visible to the process started by Docker - for example IIS. My scheduled tasks also needed to access those same env vars.

The solution was to change the entrypoint in dockerfile to a PowerShell script, as suggested by this article.

ENTRYPOINT ["powershell.exe", "C:\\bootstrap.ps1"]

This bootstrap script can do whatever you want, and in this case, allows me to "promote" env vars passed to the process started by Docker (now this script) to machine level so they're visible to other processes in the container.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.