Packer: Variable .Vars Windows Server 2016 PowerShell Provsioner

Created on 16 Mar 2017  ยท  8Comments  ยท  Source: hashicorp/packer

Packer Version: v0.12.3

When passing {{.Vars}} to the PowerShell provisioner PACKER_BUILD_NAME does not seem to be passed therefore the syntax is not recognised.

Specifically visible here:

Windows-Server2016: At line:1 char:5
Windows-Server2016: + & { = amazon-ebs; =Windows-Server2016; c:/Windows/Temp/script. ...

Provisioner command: "execute_command": "powershell -NoProfile -ExecutionPolicy \"Bypass\" \"& { {{.Vars}}{{.Path}}; exit $LastExitCode}\""

Full Error:

==> Windows-Server2016: Provisioning with shell script: D:\Code\Packer\Windows-Server2016/scripts/test.ps1
    Windows-Server2016: = : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    Windows-Server2016: spelling of the name, or if a path was included, verify that the path is correct and try again.
    Windows-Server2016: At line:1 char:5
    Windows-Server2016: + & { = amazon-ebs; =Windows-Server2016; c:/Windows/Temp/script. ...
    Windows-Server2016: +     ~
    Windows-Server2016: + CategoryInfo          : ObjectNotFound: (=:String) [], CommandNotFoundException
    Windows-Server2016: + FullyQualifiedErrorId : CommandNotFoundException
    Windows-Server2016:
    Windows-Server2016: =Windows-Server2016 : The term '=Windows-Server2016' is not recognized as the name of a cmdlet,
    Windows-Server2016: function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
    Windows-Server2016: path is correct and try again.
    Windows-Server2016: At line:1 char:19
    Windows-Server2016: + & { = amazon-ebs; =Windows-Server2016; c:/Windows/Temp/script. ...
    Windows-Server2016: +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~
    Windows-Server2016: + CategoryInfo          : ObjectNotFound: (=Windows-Server2016:String) [], CommandNotFoundException
    Windows-Server2016: + FullyQualifiedErrorId : CommandNotFoundException
    Windows-Server2016:
provisionepowershell

Most helpful comment

it would be great if the executionpolicy and noprofile were easily controlled via the provisioner.

All 8 comments

This also happens on Windows10 Anniversary Update.

This isn't a bug with Packer, the issue is with the custom execute_command. Since the execute_command is kicking off a new powershell.exe, it's evaluating the environment vars. In other words Powershell is turning $env:PACKER_BUILD_NAME into an empty string. You'll also notice that Powershell is evaluating the double quotes around the env var _values_, in effect stripping them.

IMO, the best course of action is to directly call the script using an execute command like so:

Set-ExecutionPolicy Bypass -Scope Process -Force; if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'};{{.Vars}}&'{{.Path}}';exit $LastExitCode"

This takes the builtin Packer execute command and prepends a temporary Set-ExecutionPolicy call for the duration of the process.

This provisioner previously worked so no idea if it was moving from 2012R2 or a later version of Packer that introduced the problem (I'm not sure what version I was using when this last worked).

I didn't realise the execute_command was PowerShell, I thought it was native cmd shell that invoked powershell.exe.

I can confirm that the above works as expected however I still get <AV>Preparing modules for first use.</AV> on the output unless I pass -NoProfile. $ProgressPreference = 'SilentlyContinue' as mentioned in #4322 does not help there. If wish to suppress that, I am always going to have to execute powershell.exe -NoProfile and therefore I will always need to escape the quotes.

Right now the .Vars are not important so I can get by. If I need to pass data in I might just source a file.

Thanks for your help.

it would be great if the executionpolicy and noprofile were easily controlled via the provisioner.

@rismoney I agree that seems useful, but I think it's not related to the ticket. Would you mind opening a new issue requesting that so it doesn't get lost here?

I just made a PR with the feature @rismoney suggested, but even when I set NoProfile there I still get the "Preparing modules for first use" output.

What is it about this output that's causing a problem? It's noisy but even official azure docs that explain how to use packer have it in their "expected output" docs: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/build-image-with-packer

I'm going to close this issue since underneath it all, it looks like the real problem is that we can't suppress the XML output stream, which already has its own issue here: https://github.com/hashicorp/packer/issues/4322

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings