3

I am following this guide to prepare an instance for AMI creation ("To manually run Sysprep using EC2Launch" section): https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch.html#ec2launch-sysprep

When I run the script in step 7 (./SysprepInstance.ps1) it throws an error about setting variables (something about not being able to set those variables because they are read-only). This error comes from lines 37,38,39:

Set-Variable rootPath -Option Constant -Scope Local -Value (Join-Path $env:ProgramData -ChildPath "Amazon\EC2-Windows\Launch")
Set-Variable modulePath -Option Constant -Scope Local -Value (Join-Path $rootPath -ChildPath "Module\Ec2Launch.psd1")
Set-Variable scriptPath -Option Constant -Scope Local -Value (Join-Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name)

Those lines seem to conflict with lines 64,65,66 of ./InitializeInstance.ps1:

Set-Variable rootPath -Option Constant -Scope Local -Value (Join-Path $env:ProgramData -ChildPath "Amazon\EC2-Windows\Launch")
Set-Variable modulePath -Option Constant -Scope Local -Value (Join-Path $rootPath -ChildPath "Module\Ec2Launch.psd1")
Set-Variable scriptPath -Option Constant -Scope Local -Value (Join-Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name)

On the guide I mentioned above, it suggests running those scripts manually (opening a prompt in the right folder and running those scripts). It works fine when I run them manually as I can close the terminal before running ./SysprepInstance.ps1. But when I run them via the UserData, I believe ./InitializeInstance.ps1 sets those variables to read only and then ./SysprepInstance.ps1 throws the error. This results in a bad AMI where the start menu does not work, all the terminals are weird (powershell has a black background and you cannot scroll, the cmd prompt has the Insert key of the keyboard enabled by default, and other oddities).

Here is my user data script:

<powershell>
    cd "C:\ProgramData\Amazon\EC2-Windows\Launch\\Scripts\"
    ./InitializeInstance.ps1 -Schedule
    ./SysprepInstance.ps1
<powershell/>

Does anyone have any experiance on how to prepare a Windows 2016 instance for AMI creation via the UserData?

Kappacake
  • 1,826
  • 19
  • 38

1 Answers1

0

Try without the ./SysprepInstance.ps1 also PS uses unix style dir slashes. cd "C:/ProgramData/Amazon/EC2-Windows/Launch/Scripts/"

  • SysprepInstance.ps1 MUST be run to create an AMI from a running Windows instance, so not running it is not an option. Moreover, the entire post is about that script, so just not running is not a solution... Finally, PS is perfectly happy with locations defined using backslashes (e.g. the command "cd C:\Users\" works fine) – Kappacake Dec 16 '20 at 10:42