简体   繁体   中英

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.

<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:

PowerShell is not a service running on Windows, though it does have service dependency on WinRM and a component dependency of WMI. PowerShell is a set of .exe's that you have to specifically startup to do anything with.

  1. powershell_ise.exe for scripting efforts via this UI (Windows Only)
  2. powershell.exe (Windows Only)
  3. 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. That is using a PowerShell job or PowerShell logon script.

# 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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