Accessing Environment Variables in a Scheduled Task

15

2

When setting up a scheduled task, for the action section I'm trying to access an environment variable for the location of the executable that I wish to run. This is mainly because on one machine the executable could be in C: on another it could be on D: I've verified that if I use a hard coded path my scheduled task runs, but if I change the action to point to "%MyAppPath%\MyApp.exe" the task won't start suffering a launch failure. I've verified that the environment variable is set up.

Is it even possible inside the action (and working directory) part of task schedule to reference an environment variable - is my syntax wrong.

On a side note, I did think about calling a BAT file and referencing the environment variable there, but that doesn't solve anything for me as the main issue is actually knowning whether the application (and the bat file too) is on C: or D: drives - inside the drive itself the path is the same.

UPDATE: It turns out you can use them with a machine reboot but perhaps there is a better way

Paul Hadfield

Posted 2011-09-02T10:48:48.150

Reputation: 253

Answers

23

You do not need a machine reboot. You should terminate Taskeng.exe and the next time scheduled task is run it will get an updated environment.

tsvayer

Posted 2011-09-02T10:48:48.150

Reputation: 346

Thank you so mcuh! I've been struggling with it for days. I've been trying to restart the Scheduler service to no avail in Win7/Win2008 (need to run as SYSTEM to restart it), but this is so much simpler! – Timur – 2014-05-15T14:47:31.017

2

I thought that you could reference environment variables from the Task Scheduler, but having just tried, it doesn't look like you can.

The one exception appears to be %PATH% so would it be possible to add your MyAppPath value to the %PATH% collection on each machine, then then just call MyApp.exe from the task scheduler, where the machine will be able to resolve the fully-qualified path as required?

Stuart McLaughlin

Posted 2011-09-02T10:48:48.150

Reputation: 942

1Interesting, I just tried with %PATH% and could not get it to work. At that point I noticed a stupid typo when I'd updated %PATH%. I fixed the typo and verified that PATH was OK in a DOS box, but task scheduled still reported the problem - indicating it was seeing the "bad" version of %PATH%. Rebooting was needed to pick up the change (as you can't restart task scheduler). At that point it turned out I could now use my original environment variable. So the upshot is that you can use environment variablees, but a full machine reboot is needed to pick up any changes/new environment variables – Paul Hadfield – 2011-09-02T11:39:45.087

Will mark this as the answer as you were correct that you can use them but if task scheduler has run before you modified the job / added the reference to an environment variable, it will need a reboot of the machine. – Paul Hadfield – 2011-09-02T11:41:08.457

2See tsvayer's answer - you don't need to reboot the machine. Just the task scheduler process. – Ben Challenor – 2013-09-10T11:25:52.417

1

On a side note, I did think about calling a BAT file and referencing the environment variable there, but that doesn't solve anything for me as the main issue is actually knowning whether the application (and the bat file too) is on C: or D: drives - inside the drive itself the path is the same.

%~d0 will expand to the drive letter of where the batch file is located. (%~dp0 for drive+directory, and so on.)

user1686

Posted 2011-09-02T10:48:48.150

Reputation: 283 655

1

tsvayer's answer didn't quite work for me, on a computer running Windows 7, but it pointed me in the right direction. Task Scheduler seems to be a service on my computer; it's name is Schedule; the display name is Task Scheduler.

Besides restarting it from the Services MMC, it can be restarted with the following wmic commands run from a Command Prompt window (with administrator privileges):

wmic service where "name='Schedule'" call StopService
wmic service where "name='Schedule'" call StartService

You can of course also restart the service using sc:

sc stop Schedule
sc start Schedule

Based on trial and error, it seems sufficient to disable and then re-enable a single task, if that's all you need to affect:

schtasks /Change /TN \"The name of the task\" /DISABLE
schtasks /Change /TN \"The name of the task\" /ENABLE

Kenny Evitt

Posted 2011-09-02T10:48:48.150

Reputation: 273

0

On a side note, I did think about calling a BAT file and referencing the environment variable there, but that doesn't solve anything for me as the main issue is actually knowing whether the application (and the bat file too) is on C: or D: drives - inside the drive itself the path is the same.

Expanding upon grawity's answer, I use a two step process. This allows me to use the path in other statements when needed.

SET WD=%~dp0
pushd %WD%

NOTE: pushd changes both drive and directory. And if you wanted to be clean, popd when you're done.

Richard

Posted 2011-09-02T10:48:48.150

Reputation: 1