hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.09k stars 3.33k forks source link

Powershell provisioner not working #5697

Closed Chan9390 closed 6 years ago

Chan9390 commented 6 years ago

Packer version: 1.1.3 Host platform: Ubuntu 16.04

Powershell provisioning executes unwanted strings.

I had an issue with Chef-Solo provisioning on Windows Server 2008 R2 (#5220). So I tried an alternative way to install chef-solo using powershell.

I wrote the code:

    {
      "type": "powershell",
      "inline": [". { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install"]
    }

When I executed packer, I got the following error:

==> virtualbox-iso: Provisioning with Powershell...
==> virtualbox-iso: Provisioning with powershell script: /tmp/packer-powershell-provisioner480464086
    virtualbox-iso: =SilentlyContinue : The term '=SilentlyContinue' is not recognized as the name
    virtualbox-iso: of a cmdlet, function, script file, or operable program. Check the spelling of
    virtualbox-iso: 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'};:PACKE ...
    virtualbox-iso: +                                               ~~~~~~~~~~~~~~~~~~~
    virtualbox-iso:     + CategoryInfo          : ObjectNotFound: (=SilentlyContinue:String) [], C
    virtualbox-iso:    ommandNotFoundException
    virtualbox-iso:     + FullyQualifiedErrorId : CommandNotFoundException
    virtualbox-iso:
    virtualbox-iso: :PACKER_BUILDER_TYPE=virtualbox-iso : The term
    virtualbox-iso: ':PACKER_BUILDER_TYPE=virtualbox-iso' is not recognized as the name of a
    virtualbox-iso: cmdlet, function, script file, or operable program. Check the spelling of the
    virtualbox-iso: name, or if a path was included, verify that the path is correct and try again.
    virtualbox-iso: At line:1 char:76
    virtualbox-iso: + ... e){='SilentlyContinue'};:PACKER_BUILDER_TYPE="virtualbox-iso"; :PACKE ...
    virtualbox-iso: +                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    virtualbox-iso:     + CategoryInfo          : ObjectNotFound: (:PACKER_BUILDER_TYPE=virtualbox
    virtualbox-iso:    -iso:String) [], CommandNotFoundException
    virtualbox-iso:     + FullyQualifiedErrorId : CommandNotFoundException
    virtualbox-iso:
    virtualbox-iso: :PACKER_BUILD_NAME=virtualbox-iso : The term
    virtualbox-iso: ':PACKER_BUILD_NAME=virtualbox-iso' is not recognized as the name of a cmdlet,
    virtualbox-iso: function, script file, or operable program. Check the spelling of the name, or
    virtualbox-iso: if a path was included, verify that the path is correct and try again.
    virtualbox-iso: At line:1 char:115
    virtualbox-iso: + ... ER_TYPE="virtualbox-iso"; :PACKER_BUILD_NAME="virtualbox-iso"; &'c:/W ...
    virtualbox-iso: +                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    virtualbox-iso:     + CategoryInfo          : ObjectNotFound: (:PACKER_BUILD_NAME=virtualbox-i
    virtualbox-iso:    so:String) [], CommandNotFoundException
    virtualbox-iso:     + FullyQualifiedErrorId : CommandNotFoundException
    virtualbox-iso:
    virtualbox-iso:
    virtualbox-iso: ModuleType Version    Name                                ExportedCommands
    virtualbox-iso: ---------- -------    ----                                ----------------
    virtualbox-iso: Script     0.0        Omnitruck                           {Get-ProjectMetada...

The output stops here and chef executable is not downloaded.

SwampDragons commented 6 years ago

I can't reproduce on an aws instance. Will try to see if I can reproduce on your metasploitable branch instead.

Worked with this:

    "variables": {
        "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
        "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
        "region":         "{{env `AWS_DEFAULT_REGION`}}"
    },
    "builders": [
    {
        "type": "amazon-ebs",
        "region": "us-east-1",
        "instance_type": "t2.micro",
          "source_ami_filter": {
            "filters": {
              "virtualization-type": "hvm",
              "name": "*Windows_Server-2008-R2*English-64Bit-Base*",
              "root-device-type": "ebs"
            },
            "most_recent": true,
            "owners": "amazon"
          },
        "ami_name": "default-packer{{ packer_version }}",
        "user_data_file": "./boot_config/packer_bootstrap_win.txt",
        "communicator": "winrm",
        "winrm_username": "Administrator",
        "winrm_password": "SuperS3cr3t!"
    }
    ],
    "provisioners": [

        {
            "type": "powershell",
            "inline": [". { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex; install"]
        }
    ]
}
SwampDragons commented 6 years ago

I believe the ugly output is a separate problem from the executable freezing, since that executable also fails with the chef provisioner itself. The output didn't prevent the executable from beginning and freezing in the same point as it did with the provisioner.

SwampDragons commented 6 years ago

I can't reproduce this on your metasploitable branch with 1.1.3. Does this happen every time?

SwampDragons commented 6 years ago

and I guess to be clear, I ran it with only the provisioners below to save time

"provisioners": [
    {
      "type": "windows-restart"
    },
    {
      "type": "powershell",
      "inline": [". { iwr -useb https://omnitruck.chef.io/install.ps1 } | iex"]
    },
    {
      "type": "windows-restart"
    }
  ],
SwampDragons commented 6 years ago

So I screwed up the powershell command in the above example that I forgot the install tag, which is probably why it successfully ran. This isn't a flaw with Powershell but with the command itself. I'm closing this ticket as powershell seems to actually work fine, and I'm gonna focus on your other ticket about the chef install, which is observable both when calling it from powershell (as you're doing here) and when you're calling it from the chef-solo provisioner.

alpana17 commented 5 years ago

I am facing the same issue. Packer version: 1.1.3 Provisioner:- { "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 }\"", "environment_vars": ["ProgramData=C:\ProgramData", "windir=C:\Windows"], "inline": [ "$NAME = Get-Content 'C:\ProgramData\Amazon\EC2-Windows\Launch\Config\LaunchConfig.json' -raw | ConvertFrom-Json", "$NAME.setComputerName=$true", "$NAME.adminPasswordType=\"Random\"", "$NAME| ConvertTo-Json | Set-content 'C:\ProgramData\Amazon\EC2-Windows\Launch\Config\LaunchConfig.json'", "C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule" ] }

Issue faced:- amazon-ebs: . : The term ':PACKER_BUILDER_TYPE=amazon-ebs' is not recognized as the name of a cmdlet, function, script file, or amazon-ebs: operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try amazon-ebs: again. amazon-ebs: At line:1 char:138 amazon-ebs: + ... value 'SilentlyContinue'};. :PACKER_BUILDER_TYPE="amazon-ebs"; :PACKE ... amazon-ebs: + ~~~~~~~~~ amazon-ebs: + CategoryInfo : ObjectNotFound: (:PACKER_BUILDER_TYPE=amazon-ebs:String) [], CommandNotFoundException amazon-ebs: + FullyQualifiedErrorId : CommandNotFoundException amazon-ebs: amazon-ebs: :PACKER_BUILD_NAME=amazon-ebs : The term ':PACKER_BUILD_NAME=amazon-ebs' is not recognized as the name of a cmdlet, amazon-ebs: function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the amazon-ebs: path is correct and try again. amazon-ebs: At line:1 char:173 amazon-ebs: + ... ER_BUILDER_TYPE="amazon-ebs"; :PACKER_BUILD_NAME="amazon-ebs"; :Progr ... amazon-ebs: + ~~~~~~~ amazon-ebs: + CategoryInfo : ObjectNotFound: (:PACKER_BUILD_NAME=amazon-ebs:String) [], CommandNotFoundException amazon-ebs: + FullyQualifiedErrorId : CommandNotFoundException amazon-ebs: amazon-ebs: :ProgramData=C:\ProgramData : The term ':ProgramData=C:\ProgramData' is not recognized as the name of a cmdlet, amazon-ebs: function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the amazon-ebs: path is correct and try again. amazon-ebs: At line:1 char:206 amazon-ebs: + ... PACKER_BUILD_NAME="amazon-ebs"; :ProgramData="C:\ProgramData"; :windi ... amazon-ebs: + ~~~~~~~~~ amazon-ebs: + CategoryInfo : ObjectNotFound: (:ProgramData=C:\ProgramData:String) [], CommandNotFoundException amazon-ebs: + FullyQualifiedErrorId : CommandNotFoundException amazon-ebs: amazon-ebs: :windir=C:\Windows : The term ':windir=C:\Windows' is not recognized as the name of a cmdlet, function, script file, amazon-ebs: or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and amazon-ebs: try again. amazon-ebs: At line:1 char:237 amazon-ebs: + ... zon-ebs"; :ProgramData="C:\ProgramData"; :windir="C:\Windows"; ; &'c: ... amazon-ebs: + ~~~~ amazon-ebs: + CategoryInfo : ObjectNotFound: (:windir=C:\Windows:String) [], CommandNotFoundException amazon-ebs: + FullyQualifiedErrorId : CommandNotFoundException amazon-ebs: amazon-ebs: Join-Path : Cannot bind argument to parameter 'Path' because it is null. amazon-ebs: At C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1:55 char:71 amazon-ebs: + ... tion Constant -Scope Local -Value (Join-Path $env:ProgramData -ChildP ... amazon-ebs: + ~~~~ amazon-ebs: + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException amazon-ebs: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom amazon-ebs: mand amazon-ebs: amazon-ebs: Join-Path : Cannot bind argument to parameter 'Path' because it is null. amazon-ebs: At C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1:56 char:73 amazon-ebs: + ... ath -Option Constant -Scope Local -Value (Join-Path $rootPath -ChildP ... amazon-ebs: + ~~~~~ amazon-ebs: + CategoryInfo : InvalidData: (:) [Join-Path], ParameterBindingValidationException amazon-ebs: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom amazon-ebs: mand amazon-ebs: amazon-ebs: Import-Module : Cannot bind argument to parameter 'Name' because it is null. amazon-ebs: At C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1:65 char:15 amazon-ebs: + Import-Module $modulePath amazon-ebs: + ~~~ amazon-ebs: + CategoryInfo : InvalidData: (:) [Import-Module], ParameterBindingValidationException amazon-ebs: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ImportModul amazon-ebs: eCommand

SwampDragons commented 5 years ago

Looks like you execute_command isn't setting the variables properly. Does it work for you if you remove your custom execute_command?

ghost commented 4 years ago

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.