My goal is to run powershell commands on EC2 Windows Server 2016 instance at launch in order to set multiple system paths permanently.
<powershell>
######### SET SYSTEM PATHS #########
# We need to restart the compute in order for the system paths to take effect.
# Source: https://www.quora.com/Why-wont-Python-work-in-PowerShell-for-me
### aws ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + “;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Lib\site-packages\awscli", "Machine")
### python ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Users\Administrator\AppData\Local\Programs\Python\Python36", "Machine")
### pip ###
[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + “;C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts", "Machine")
</powershell>
The code works perfectly fine when we manually run the script and making sure to restart the powershell console. Note that the paths don't necessarily need to exists in order to be set.
However, when running the code systematically when the instance is initialised via EC2 doesn't work. Adding Restart-Computer
as an attempt to restart powershell console that I'm assuming is running behind the scenes does not work.
Additional sources: