【发布时间】:2018-06-24 16:49:58
【问题描述】:
我的目标是run powershell commands on EC2 Windows Server 2016 instance at launch,以便永久设置多个系统路径。
<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>
当我们手动运行脚本并确保重新启动 powershell 控制台时,代码运行良好。请注意,路径不一定需要存在才能设置。
但是,在通过 EC2 初始化实例时系统地运行代码时不起作用。添加Restart-Computer 以尝试重新启动我假设在幕后运行的powershell 控制台不起作用。
其他来源:
【问题讨论】:
标签: python windows powershell amazon-ec2 environment-variables