Getting Task Scheduler to see new environment variables
I recently encountered a opportunity that required me to set an environment variable in my user data script for my EC2 instance, and then I had a scheduled task that started to run immediately. The scheduled task was never able to see the environment variable unless I rebooted the instance. EC2 is capable of handling reboots as long as it is the last line in your user data. But that is ugly and takes much longer, especially considering I am running 2016 base on a t2.micro instance. Because my application is a series of scripts and nothing more.Setting the environment variable works flawlessly via this PowerShell command.
[Environment]::SetEnvironmentVariable(‘MachineEnviroment’,”$Environment”,’Machine’)
The task scheduler service “Schedule” is already started before this environment variable is set. I was able to find a trick of abusing the SYSTEM account and restarting the Schedule service. In my userdata file I added the following command which executes as SYSTEM because all userdata files from EC2 on Windows always executes as SYSTEM without any special configuration.
Get-Service Schedule | Restart-Service
You can see what environment variables that a process has loaded with Process Explorer from Sys Internals.
Sources