0

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:

Elrond
  • 347
  • 2
  • 15

2 Answers2

0

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
postanote
  • 15,138
  • 2
  • 14
  • 25
  • I’m specifically referring to powershell run when creating the EC2 instance. The methodology is indicated in the first link. – Elrond Jan 16 '18 at 09:17
  • Yep, I've seen, but that doc must be paired with technical guidance of their other guidance, https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/UsingConfig_WinAMI.html. So, are you saying, you've tried the direction in the above and it's still not working? Notes also show that User data scripts are executed by the Ec2Config service which runs in the SYSTEM context, so, this begs the question, what context are you using? Full Disclosure, this is something I only tried in a class a long time ago. Never in production, PoSH DSC is my go to for that, or pre-bake your AMI with this need. – postanote Jan 16 '18 at 10:27
0

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.

  • If you checkout the section "User Data and the Console" in my first hyperlink, you'll see that I can only provide a powershell script. Put in other words, there is no path to the script to be provided and I don't see any execution policy flag che creating the EC2 instance. I don't want to manually run any powershell script after having initialised the instance. – Elrond Jan 16 '18 at 22:02