From f4a2ac3b31d6bb4d7c512a0fa638048ca34226f9 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com> Date: Fri, 19 Aug 2022 16:00:46 -0400 Subject: [PATCH] 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 --- provisioner/powershell/provisioner.go | 10 +++++++++- provisioner/powershell/provisioner.hcl2spec.go | 2 ++ provisioner/powershell/provisioner_test.go | 18 ++++++++++++++++++ .../content/docs/provisioners/powershell.mdx | 2 ++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index fb11b48d1..396eafc21 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -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() } diff --git a/provisioner/powershell/provisioner.hcl2spec.go b/provisioner/powershell/provisioner.hcl2spec.go index 04109019e..2cf0e26dd 100644 --- a/provisioner/powershell/provisioner.hcl2spec.go +++ b/provisioner/powershell/provisioner.hcl2spec.go @@ -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 } diff --git a/provisioner/powershell/provisioner_test.go b/provisioner/powershell/provisioner_test.go index 5499beec1..ac0b65610 100644 --- a/provisioner/powershell/provisioner_test.go +++ b/provisioner/powershell/provisioner_test.go @@ -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() diff --git a/website/content/docs/provisioners/powershell.mdx b/website/content/docs/provisioners/powershell.mdx index 1000748b4..a5be652ed 100644 --- a/website/content/docs/provisioners/powershell.mdx +++ b/website/content/docs/provisioners/powershell.mdx @@ -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