feat: support pwsh in powershell provisioner (#11950)

* set `pwsh` to true in config to run `pwsh.exe`, default is false.
* add docs

Co-authored-by: Ed Eustace <ed@smashcut.com>
pull/11953/head
Lucas Bajolet 4 years ago committed by GitHub
parent 25e6546bb1
commit f4a2ac3b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -89,6 +89,9 @@ type Config struct {
// A duration of how long to pause after the provisioner
PauseAfter time.Duration `mapstructure:"pause_after"`
// Run pwsh.exe instead of powershell.exe - latest version of powershell.
UsePwsh bool `mapstructure:"use_pwsh"`
ctx interpolate.Context
}
@ -112,7 +115,12 @@ func (p *Provisioner) defaultExecuteCommand() string {
return baseCmd
}
return fmt.Sprintf(`powershell -executionpolicy %s "%s"`, p.config.ExecutionPolicy, baseCmd)
if p.config.UsePwsh {
return fmt.Sprintf(`pwsh -executionpolicy %s -command "%s"`, p.config.ExecutionPolicy, baseCmd)
} else {
return fmt.Sprintf(`powershell -executionpolicy %s "%s"`, p.config.ExecutionPolicy, baseCmd)
}
}
func (p *Provisioner) ConfigSpec() hcldec.ObjectSpec { return p.config.FlatMapstructure().HCL2Spec() }

@ -38,6 +38,7 @@ type FlatConfig struct {
ExecutionPolicy *string `mapstructure:"execution_policy" cty:"execution_policy" hcl:"execution_policy"`
DebugMode *int `mapstructure:"debug_mode" cty:"debug_mode" hcl:"debug_mode"`
PauseAfter *string `mapstructure:"pause_after" cty:"pause_after" hcl:"pause_after"`
UsePwsh *bool `mapstructure:"use_pwsh" cty:"use_pwsh" hcl:"use_pwsh"`
}
// FlatMapstructure returns a new FlatConfig.
@ -80,6 +81,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"execution_policy": &hcldec.AttrSpec{Name: "execution_policy", Type: cty.String, Required: false},
"debug_mode": &hcldec.AttrSpec{Name: "debug_mode", Type: cty.Number, Required: false},
"pause_after": &hcldec.AttrSpec{Name: "pause_after", Type: cty.String, Required: false},
"use_pwsh": &hcldec.AttrSpec{Name: "use_pwsh", Type: cty.Bool, Required: false},
}
return s
}

@ -273,6 +273,24 @@ func TestProvisionerPrepare_Scripts(t *testing.T) {
}
}
func TestProvisionerPrepare_Pwsh(t *testing.T) {
config := testConfig()
config["use_pwsh"] = true
p := new(Provisioner)
err := p.Prepare(config)
if err != nil {
t.Fatalf("Should not be error: %s", err)
}
if !p.config.UsePwsh {
t.Fatalf("Expected 'pwsh' to be: true")
}
}
func TestProvisionerPrepare_EnvironmentVars(t *testing.T) {
config := testConfig()

@ -86,6 +86,8 @@ provisioner "powershell" {
Packer injects some environmental variables by default into the
environment, as well, which are covered in the section below.
- `use_pwsh` (boolean) - Run `pwsh.exe` instead of `powershell.exe`. Defaults to false.
This is a [template engine](/docs/templates/legacy_json_templates/engine). Therefore, you
may use user variables and template functions in this field. If you are
running on AWS, Azure, Google Compute, or OpenStack and would like to access

Loading…
Cancel
Save