Windows Task Scheduler job with parameter and STDOUT redirect
c#, scheduled-tasks, windows
Solution
Output redirection may or may not work with the Windows Task Scheduler. The workaround is to run your desired command (including output redirection) inside a batch file, and to call the batch file from Task Scheduler.
script.bat
----------
myprogram SomeValue > results.log
Problem
I have a command line program that expects either no parameter or one parameter. If no parameter is supplied it prompts for the parameter with simple code like: ``` String theParameter = String.Empty; if (args.Length == 1) theParameter = args[0]; else { Console.Write("Please provide theParameter: "); theParameter = Console.ReadLine(); } Console.WriteLine("Some Output"); ``` Interactively it works as expected: ``` > myprogram Please provide theParameter: {a value provided} Some Output ``` or ``` > myprogram SomeValue Some Output ``` or ``` > myprogram SomeValue > results.log {Some Output in the results.log file) ``` All work as expected. Similarly when I use the Windows 7 Task Scheduler with `myprogram SomeValue` it starts, executes and completes as expected. However when I use `myprogram SomeValue > results.log` to redirect STDOUT to a file it starts, runs and never completes. If I manually run the job (by right clicking and running from the Task Scheduler) it throws up a console window with the `Please provide the Parameter`. My question is: Why is the Windows Task Scheduler job short-circuiting my parameter being passed to the program if I redirect STDOUT to a file?