Accessing Environment Variables in a Scheduled Task
Solution 1:
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.
Solution 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?
Solution 3:
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.)
Solution 4:
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