Packer: Powershell provisioner broken by default. "'=SilentlyContinue' is not recognized"

Created on 10 Oct 2018  ·  12Comments  ·  Source: hashicorp/packer

Packer version: v1.3.1
Host Machine: Windows 7 64-bit

I am running Packer using the VirtualBox builder to try and build a Windows 10 box from an ISO. I am using the latest Windows 10 build (1809).

I have tried running both a Powershell script and an inline Powershell command using the default Powershell provisioner. Both return an error about SilentlyContinue not being recognized.

Example:

{
    "type": "powershell",
    "inline": ["Import-StartLayout -LayoutPath C:/Windows/Temp/layoutModification.xml -MountPath C:\\"]
},

Output:

==> virtualbox-iso: Provisioning with Powershell...
==> virtualbox-iso: Provisioning with powershell script: E:\TEMP\packer-powershell-provisioner453446655
    virtualbox-iso: =SilentlyContinue : The term '=SilentlyContinue' is not recognized as the name of a cmdlet, function, script file, or
    virtualbox-iso: operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    virtualbox-iso: again.
    virtualbox-iso: At line:1 char:55
    virtualbox-iso: + ... -Path variable:global:ProgressPreference){='SilentlyContinue'};. c:/W ...
    virtualbox-iso: +                                               ~~~~~~~~~~~~~~~~~~~
    virtualbox-iso:     + CategoryInfo          : ObjectNotFound: (=SilentlyContinue:String) [], CommandNotFoundException
    virtualbox-iso:     + FullyQualifiedErrorId : CommandNotFoundException
    virtualbox-iso:
bug provisionepowershell

Most helpful comment

It looks like that worked!!

{
      "type": "powershell",
      "execute_command": "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}';exit $LastExitCode }\"",
      "scripts": [
        "./scripts/remove-windows-features.ps1",
        "./scripts/install-vs2017.ps1"
      ],
      "pause_before": "2m"
    }

(I had tried using pause_before previously and it had no effect.

==> virtualbox-iso: Provisioning with powershell script: ./scripts/install-vs2017.ps1
         virtualbox-iso: The command completed successfully.

Looks like that command solved the problem. 👍

All 12 comments

Interesting. The default powershell invocation contains a check:

if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'}

Based on your error message, $ProgressPreference isn't set. But if that's the case, (Test-Path variable:global:ProgressPreference) shouldn't be true.

I'll see if I can figure out how to reproduce.

I think this is a duplicate of #5772 but I'm not convinced the answer I gave there is correct.

However, if the issue is that the system hasn't fully finished sysprepping, you can figure that out by adding a pause_before of 30s or a minute to your template, just to see if the problem goes away when that's been done.

So I can try adding pause_before before running the script. However, I _believe_ this script is run after some time has past from installing Windows. For example I have scripts running Windows updates and things like that before hand. These run before I try to use the Powershell provisioner.

Makes sense. Worth ruling out, but I am not surprised it wasn't the answer. Now to figure out why that preference variable isn't set correctly for you. Maybe if it's set to an empty string it passes the check but fails being written to?

Okay, I'm struggling to reproduce so if you're okay with it I'd like you to test something for me.

What happens if you run this with the custom
"execute_command": "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){ if (variable:global:ProgressPreference -ne '' -and variable:global:ProgressPreference -ne $null) {$ProgressPreference='SilentlyContinue'}};. {{.Vars}}; &'{{.Path}}';exit $LastExitCode }\""?

Actually that one won't work-- I'll update in a minute with a better one.

Okay, try this: powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){ $val=(gi "variable:global:ProgressPreference").value; if ($val -ne '' -and $val -ne $null) {$ProgressPreference='SilentlyContinue'}};. {{.Vars}}; &'{{.Path}}';exit $LastExitCode }\"

And actually, maybe this is better:
powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}';exit $LastExitCode }\"

It looks like that worked!!

{
      "type": "powershell",
      "execute_command": "powershell -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};. {{.Vars}}; &'{{.Path}}';exit $LastExitCode }\"",
      "scripts": [
        "./scripts/remove-windows-features.ps1",
        "./scripts/install-vs2017.ps1"
      ],
      "pause_before": "2m"
    }

(I had tried using pause_before previously and it had no effect.

==> virtualbox-iso: Provisioning with powershell script: ./scripts/install-vs2017.ps1
         virtualbox-iso: The command completed successfully.

Looks like that command solved the problem. 👍

Awesome! I'll change the defaults in the provisioner and make a PR.

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