简体   繁体   English

初始化EC2实例时永久设置Windows PowerShell系统路径

[英]Setting Windows PowerShell system path permanently when initialising an EC2 instance

My goal is to run powershell commands on EC2 Windows Server 2016 instance at launch in order to set multiple system paths permanently. 我的目标是在启动时EC2 Windows Server 2016实例上运行powershell命令 ,以便永久设置多个系统路径。

<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. 当我们手动运行脚本并确保重新启动Powershell控制台时,代码可以正常工作。 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. 但是,当通过EC2初始化实例时系统地运行代码时不起作用。 Adding Restart-Computer as an attempt to restart powershell console that I'm assuming is running behind the scenes does not work. 添加Restart-Computer作为尝试重新启动在后台运行的Powershell控制台的尝试不起作用。

Additional sources: 其他来源:

PowerShell is not a service running on Windows, though it does have service dependency on WinRM and a component dependency of WMI. PowerShell不是Windows上运行的服务,尽管它确实具有WinRM的服务依赖性和WMI的组件依赖性。 PowerShell is a set of .exe's that you have to specifically startup to do anything with. PowerShell是一组.exe,您必须专门启动才能执行任何操作。

  1. powershell_ise.exe for scripting efforts via this UI (Windows Only) powershell_ise.exe通过此UI进行脚本编写工作(仅Windows)
  2. powershell.exe (Windows Only) powershell.exe(仅Windows)
  3. pwsh.exe (PowerShell Core, Win, OSX, Linux) pwsh.exe(PowerShell Core,Win,OSX,Linux)

If you are trying to execute and .ps1 on startup, use a start-up script, using GPO or not. 如果尝试在启动时执行.ps1,请使用启动脚本(是否使用GPO)。 That is using a PowerShell job or PowerShell logon script. 那就是使用PowerShell作业或PowerShell登录脚本。

# Use PowerShell to Create Job that Runs at Startup
# 
# Beginning with Windows PowerShell 3.0, with Windows PowerShell scheduled jobs, it is possible to natively and easily create a Windows PowerShell startup script (and one that does not rely on Group Policy).
# 
# A few steps are required to create a Windows PowerShell script that runs at startup as a Windows PowerShell scheduled job:
# 1. Open the Windows PowerShell console with admin rights.
# 2. Create a new job trigger and specify the type as a startup trigger.
# 3. Specify a short random interval for the startup trigger to prevent race conditions at startup.
# 4. Create the new scheduled job and specify the job trigger and the full path to the startup script.
# 
# 
# https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/14/use-powershell-to-create-job-that-runs-at-startup

# Use Startup, Shutdown, Logon, and Logoff Scripts
# You can use Windows PowerShell scripts, or author scripts in any other language supported by the client computer.
# https://technet.microsoft.com/en-us/library/cc753404(v=ws.11).aspx

powershell.exe .\\foo.ps1 powershell.exe。\\ foo.ps1

Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run. 确保指定脚本的完整路径,并确保将执行策略级别设置为至少“ RemoteSigned”,以便可以运行未签名的本地脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM