Search code examples
powershellvagrantwindows-server-2012-r2amazon-amipacker

Packer - Windows Server 2012R2 - Install PowerShell 5


I'm having trouble with installing PowerShell 5 on Windows Server 2012 R2 using Packer.

To install PowerShell 5.1, I'm using chocolatey

powershell iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex choco install PowerShell -y

I need to to do this for two packer builder:

  • Amazon Web Services EC2 AMI.
  • Hyper-V Vagrant.

With the Amazon Web Services EC2 AMI I've tried executing the above from the following locations

  • As part of the user_data_file script.
  • As an inline provision script.

I've added the windows-restart provision in the flow after the execution of the fragment.

Similarly I've tried the same with the Hyper-V Vagrant both as part of the Autounattend flow and as an inline provision.

In both cases the installation fails because the user profile is not created. At least I think that is the problem. As a reference, I've faced the exact same problem when installing sql server express.

Has anyone managed to accomplish this flow and what is your recommendation?


Solution

  • PowerShell package uses MSU (Windows Update file). There are a couple of considerations here:

    • Ensure Windows Updates Service is turned on. Chocolatey doesn't do that yet, although there is an issue filed.
    • MSUs must be run locally from the machine. So if you are running over WinRM, you instead need to have it set up a scheduled task to call choco install and then run it that way.

    Yes, that second point is accurate. No, it is not Chocolatey's fault. That's a Microsoft design decision with MSU files. It's a real PITA.

    FYI - BoxStarter handles it appropriately, and it can be used with Packer. You will find less pain if you look there.

    HTH