Search code examples
batch-filescheduled-tasksjscript

How to force a started program's tray icon to appear from a scheduled task set to run whether the user is logged on or not?


On Windows, a scheduled task set to run whether the user is logged on or not and whose batch file is launching another program will prevent that newly launched program to display its tray icon.

On the other hand, the same scheduled task set to only run if the user is logged on will spawn the program's tray icon, but will have the undesired effect of also spawning a command prompt window for a split second AND it's not the desired situation, because this scheduled task has to run even before any user logged on that computer anyway.

So, is there a way to force a launched program's tray icon to display when called from a scheduled task set to run whether the user is logged on or not? (even if this means that a specific Windows profile user/pass had to be specified in the batch file)

Here's the batch file executed from the scheduled task, note the tray icon missing is on shell.Run line at the bottom:

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "URL=http://**********HIDDEN**********.php?pull=1";
cscript /nologo /e:jscript "%~f0" "%URL%";

goto :EOF
@end

var x = WSH.CreateObject("Microsoft.XMLHTTP");

x.open("GET",WSH.Arguments(0),false,"********","********");
x.setRequestHeader('User-Agent','XMLHTTP/1.0');
x.send('');
while (x.readyState != 4) WSH.Sleep(50);

if (x.status == 200 && x.responseText == '1') {
    var shell = WScript.CreateObject("WScript.Shell");
    shell.Run('"C:\\Program Files\\TightVNC\\tvnserver.exe"');
}

Solution

  • Well, I ended up making a second scheduled task which only runs when the user is logged in and that batch file starts the program, making its tray icon visible. It runs 3 seconds after the first scheduled task which, as seen in the OP, pulls a byte from the server but then at the bottom, instead of doing a shell.Run(), it creates a cookie text file and the second scheduled task picks it up 3 seconds later.

    So when the computer is unattended/logged out of any Windows session, the first scheduled task runs a service instead of an app (no icon but who cares) but then when an user logs in a Windows session, the second scheduled task picks up the cookie text file left by the first scheduled task and pops the tray icon up.

    Works like a charm, but it's worth noting that anything launched from a scheduled task set to run on whether an user is logged in or not will NEVER display any taskbar/tray/notification icon, EVER. All subsequent programs, processes or commands bubbling from that scheduled task will be invisible to the user, like if ran under a private session, even tough the Windows account user/pass is specified in the scheduled task and that the program runs as that specified user account. It's just the way scheduled tasks are, it's kind of a limitation, IMO, but it can be overcome with the hack mentioned above.

    To prevent any kind of disruption since this scheduled task runs every 5 minutes, I used the 8KB "CMDH.exe" binary which I bundled with the batch files I deployed on the computers. http://www.gate2.net/tools/cmdh/cmdh.html