Skip to content

Commit 45c8caa

Browse files
WinOps Teamcopybara-github
authored andcommitted
Add plumbing to allow callers to choose to use pwsh7 or not
PiperOrigin-RevId: 821673727
1 parent fb2ef9d commit 45c8caa

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

powershell/powershell.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ const (
4444
type PSConfig struct {
4545
ErrAction ErrorAction // The ErrorAction that is set.
4646
Params []string // Additional parameters for calls to powershell.exe.
47-
usePwsh7 bool // Use PowerShell 7 if true.
47+
UsePwsh7 bool // Use PowerShell 7 if true.
4848
}
4949

5050
var (
5151
// defaultConfig is the default configuration for PowerShell calls.
52-
// It assumes that powershell.exe is calledc with -NoProfile and that
52+
// It assumes that powershell.exe is called with -NoProfile and that
5353
// the desired ErrorAction preference is "Stop"
5454
defaultConfig = PSConfig{
5555
ErrAction: Stop,
5656
Params: []string{"-NoProfile"},
57+
UsePwsh7: false,
5758
}
5859

5960
// ErrPowerShell represents an error returned by PowerShell.

powershell/powershell_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ func TestCommand(t *testing.T) {
4242
desc string
4343
psCmd string
4444
supplemental []string
45-
fakePowerShellCmd func([]string) ([]byte, error)
45+
fakePowerShellCmd func([]string, bool) ([]byte, error)
4646
err error
4747
}{
4848
{
4949
desc: "powershell error",
5050
psCmd: "Verb-Noun",
51-
fakePowerShellCmd: func([]string) ([]byte, error) { return nil, errors.New("powershell-error") },
51+
fakePowerShellCmd: func([]string, bool) ([]byte, error) { return nil, errors.New("powershell-error") },
5252
err: ErrPowerShell,
5353
},
5454
{
5555
desc: "supplemental error",
5656
psCmd: "Verb-Noun",
5757
supplemental: []string{"cmdlet-error"},
58-
fakePowerShellCmd: func([]string) ([]byte, error) { return []byte("cmdlet-error: details"), nil },
58+
fakePowerShellCmd: func([]string, bool) ([]byte, error) { return []byte("cmdlet-error: details"), nil },
5959
err: ErrSupplemental,
6060
},
6161
{
6262
desc: "success",
6363
psCmd: "Verb-Noun",
64-
fakePowerShellCmd: func([]string) ([]byte, error) { return []byte("output"), nil },
64+
fakePowerShellCmd: func([]string, bool) ([]byte, error) { return []byte("output"), nil },
6565
err: nil,
6666
},
6767
}

powershell/powershell_windows.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
// Dependency injection for testing.
3434
powerShellCmd = powerShell
3535

36-
powerShellExe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`)
36+
powerShellExe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`)
3737
powerShell7Exe = `C:\Program Files\PowerShell\7\pwsh.exe`
3838
)
3939

@@ -42,17 +42,21 @@ var (
4242
// The params parameter should be populated with all of the required
4343
// parameters to invoke powershell.exe from the command line. If an error is
4444
// returned to the OS, it will be returned here.
45-
func powerShell(params []string) ([]byte, error) {
46-
out, err := exec.Command(powerShellExe, params...).CombinedOutput()
45+
func powerShell(params []string, pwsh7 bool) ([]byte, error) {
46+
exe := powerShellExe
47+
if pwsh7 {
48+
exe = powerShell7Exe
49+
}
50+
out, err := exec.Command(exe, params...).CombinedOutput()
4751
if err != nil {
48-
return []byte{}, fmt.Errorf(`exec.Command(%q, %s) command returned: %q: %v`, powerShellExe, params, out, err)
52+
return []byte{}, fmt.Errorf(`exec.Command(%q, %s) command returned: %q: %v`, exe, params, out, err)
4953
}
5054
return out, nil
5155
}
5256

53-
func execute(params []string, supplemental []string) ([]byte, error) {
57+
func execute(params []string, supplemental []string, pwsh7 bool) ([]byte, error) {
5458
// Invoke PowerShell
55-
out, err := powerShellCmd(params)
59+
out, err := powerShellCmd(params, pwsh7)
5660
if err != nil {
5761
return out, fmt.Errorf("powershell returned %v: %w", err, ErrPowerShell)
5862
}
@@ -84,7 +88,7 @@ func Command(psCmd string, supplemental []string, config *PSConfig) ([]byte, err
8488
cmd := fmt.Sprintf(`$ErrorActionPreference="%s"; %s`, config.ErrAction, psCmd)
8589
params := append(config.Params, "-Command", cmd)
8690

87-
return execute(params, supplemental)
91+
return execute(params, supplemental, config.UsePwsh7)
8892
}
8993

9094
// File executes a PowerShell script file.
@@ -98,7 +102,7 @@ func File(path string, args []string, supplemental []string, config *PSConfig) (
98102
params := append(config.Params, "-File", path)
99103
params = append(params, args...)
100104

101-
return execute(params, supplemental)
105+
return execute(params, supplemental, config.UsePwsh7)
102106
}
103107

104108
// Version gathers powershell version information from the host, returns an error if version information cannot be obtained.

0 commit comments

Comments
 (0)