0

I have to think this is a solved issue but I am just not getting it to work. So I have come to you StackOverflow with this issue:

I have a windows server 2016 machine running in amazon ec2. I have a machine.ps1 script in a config directory.

I create an image of the box. (I have tried with checking noreboot and unchecking it) When I create a new instance of the image I want it to run machine.ps1 at launch to set the computer name and then set routes and some config settings for the box. The goal is to do this without logging into the box.

I have read and tried: Running Powershell scripts at Start up and used this to ensure user data was getting passed in: EC2 Powershell Launch Tools

I have tried setting up a scheduled task that runs the machine.ps1 on start up (It just hangs) I see the initializeInstance.ps1 on start up task and have tried to even coop that replacing the line to run userdata with the line to run my script. Nothing.

If I log into the box and run machine.ps1, it will restart the computer and set the computer name and then I need to run it once more to set routes. This works manually. I just need to find a way to do it automagically.

I want to launch these instances from powershell not with launch configurations and auto scale.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Chad Boyer
  • 217
  • 1
  • 4
  • 9

2 Answers2

0

You can use User data Whenever you deploy a new server, workstation or virtual machine there is nearly always a requirement to make final changes to the system before it’s ready for use. Typically this is normally done with a post-deployment script that might be triggered manually on start-up or it might be a final step in a Configuration Manager task sequence or if you using Azure you may use the Custom Script Extension. So how do you achieve similar functionality using EC2 instances in Amazon Web Services (AWS)? If you’ve created your own Amazon Machine Image (AMI) you can set the script to run from the Runonce registry key, but then can be a cumbersome approach particularly if you want to make changes to the script and it’s been embedded into the image. AWS offers a much more dynamic method of injecting a script to run upon start-up through a feature called user data.

Please refer following link for ther same:

Poershell User data

Vaibhav Walke
  • 445
  • 3
  • 6
  • I added user data as part of the pre imaged box. When I image the box the user data is gone. If I pass the user data to the create-instance the user data is still not there. Thoughts? – Chad Boyer Nov 30 '17 at 21:12
0

Windows typically won't let a powershell script call another powershell script unless it is being run as Administrator. It is a weird 'safety' feature. But it is perfectly okay to load the ps1 files and use any functions inside them.

The UserData script is typically run as "system". You would THINK that would pass muster. But it fails...

The SOLUTION: Make ALL of your scripts into powershell functions instead.

In your machine.ps1 - wrap the contents with function syntax

function MyDescriptiveName { <original script contents> }

Then in UserData - use the functions like this

# To use a relative path
Set-Location -Path <my location>
# Load script file into process memory
. <full-or-relpath>/machine.ps1
# Call function
MyDescriptiveName <params-if-applicable>

If the function needs to call other functions (aka scripts), you'll need to make those scripts into functions and load the script file into process memory in UserData also.

Robin Johnson
  • 357
  • 2
  • 11