Scheduled Task to show console window when logged on but still run when not logged on

consolescheduled-taskwindows-server-2008

Is it possible (and if so, how) to set up a task (console application) in Server 2008 so that it'll run both when a user is logged in and when no user is logged in, AND – if the user is logged in (either local or via RDP) – have the console appear on the screen while the program is running?

Ie. the program should run under the defined user context and it writes status messages to stdout, which goes to a standard console window. This console window is either shown (if the defined user is currently logged in locally or via RDP), or not shown (but the application is still run).

I have access to the source of the console application, so if it needs some additional code (like specifically opening up a new console window or what have you), then that's not a problem.

At the moment, I can set up the task as "Run only when user is logged on" which will run the application when the user is logged on (local or RDP) and I can then see the status messages, or I can set it up as "Run whether user is logged or not" and no status output is visible – not even if the user is logged on.

Best Answer

I would approach this slightly differently. I would redirect the console program's output to a log file.

for /f "usebackq tokens=1,2,3,4,5,6,7 delims=/:. " %%a in (`echo %DATE% %TIME%`) do set NOW=%%d%%b%%c_%%e%%f%%g
myprogram.exe > c:\logs\job_%NOW%.log 2>&1
rem if user is logged in, display the file
type c:\logs\job_%NOW%.log

If no one is logged in, then the last 'type' command will be inconsequential. As previously suggested, use the "Run whether user is logged or not" option.