From 0c6d5ae5c5d66325e063f40f4f66e5d2e7963b83 Mon Sep 17 00:00:00 2001 From: WinOps Team Date: Mon, 20 Oct 2025 09:25:27 -0700 Subject: [PATCH] Add plumbing to allow callers to choose to use pwsh7 or not PiperOrigin-RevId: 821679346 --- powershell/powershell.go | 5 +++-- powershell/powershell_test.go | 8 ++++---- powershell/powershell_windows.go | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/powershell/powershell.go b/powershell/powershell.go index 557981b..1f13672 100644 --- a/powershell/powershell.go +++ b/powershell/powershell.go @@ -44,16 +44,17 @@ const ( type PSConfig struct { ErrAction ErrorAction // The ErrorAction that is set. Params []string // Additional parameters for calls to powershell.exe. - usePwsh7 bool // Use PowerShell 7 if true. + UsePwsh7 bool // Use PowerShell 7 if true. } var ( // defaultConfig is the default configuration for PowerShell calls. - // It assumes that powershell.exe is calledc with -NoProfile and that + // It assumes that powershell.exe is called with -NoProfile and that // the desired ErrorAction preference is "Stop" defaultConfig = PSConfig{ ErrAction: Stop, Params: []string{"-NoProfile"}, + UsePwsh7: false, } // ErrPowerShell represents an error returned by PowerShell. diff --git a/powershell/powershell_test.go b/powershell/powershell_test.go index d1d5d73..1d4462c 100644 --- a/powershell/powershell_test.go +++ b/powershell/powershell_test.go @@ -42,26 +42,26 @@ func TestCommand(t *testing.T) { desc string psCmd string supplemental []string - fakePowerShellCmd func([]string) ([]byte, error) + fakePowerShellCmd func([]string, bool) ([]byte, error) err error }{ { desc: "powershell error", psCmd: "Verb-Noun", - fakePowerShellCmd: func([]string) ([]byte, error) { return nil, errors.New("powershell-error") }, + fakePowerShellCmd: func([]string, bool) ([]byte, error) { return nil, errors.New("powershell-error") }, err: ErrPowerShell, }, { desc: "supplemental error", psCmd: "Verb-Noun", supplemental: []string{"cmdlet-error"}, - fakePowerShellCmd: func([]string) ([]byte, error) { return []byte("cmdlet-error: details"), nil }, + fakePowerShellCmd: func([]string, bool) ([]byte, error) { return []byte("cmdlet-error: details"), nil }, err: ErrSupplemental, }, { desc: "success", psCmd: "Verb-Noun", - fakePowerShellCmd: func([]string) ([]byte, error) { return []byte("output"), nil }, + fakePowerShellCmd: func([]string, bool) ([]byte, error) { return []byte("output"), nil }, err: nil, }, } diff --git a/powershell/powershell_windows.go b/powershell/powershell_windows.go index ac23a80..9236bf9 100644 --- a/powershell/powershell_windows.go +++ b/powershell/powershell_windows.go @@ -33,7 +33,7 @@ var ( // Dependency injection for testing. powerShellCmd = powerShell - powerShellExe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`) + powerShellExe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`) powerShell7Exe = `C:\Program Files\PowerShell\7\pwsh.exe` ) @@ -42,17 +42,21 @@ var ( // The params parameter should be populated with all of the required // parameters to invoke powershell.exe from the command line. If an error is // returned to the OS, it will be returned here. -func powerShell(params []string) ([]byte, error) { - out, err := exec.Command(powerShellExe, params...).CombinedOutput() +func powerShell(params []string, pwsh7 bool) ([]byte, error) { + exe := powerShellExe + if pwsh7 { + exe = powerShell7Exe + } + out, err := exec.Command(exe, params...).CombinedOutput() if err != nil { - return []byte{}, fmt.Errorf(`exec.Command(%q, %s) command returned: %q: %v`, powerShellExe, params, out, err) + return []byte{}, fmt.Errorf(`exec.Command(%q, %s) command returned: %q: %v`, exe, params, out, err) } return out, nil } -func execute(params []string, supplemental []string) ([]byte, error) { +func execute(params []string, supplemental []string, pwsh7 bool) ([]byte, error) { // Invoke PowerShell - out, err := powerShellCmd(params) + out, err := powerShellCmd(params, pwsh7) if err != nil { return out, fmt.Errorf("powershell returned %v: %w", err, ErrPowerShell) } @@ -84,7 +88,7 @@ func Command(psCmd string, supplemental []string, config *PSConfig) ([]byte, err cmd := fmt.Sprintf(`$ErrorActionPreference="%s"; %s`, config.ErrAction, psCmd) params := append(config.Params, "-Command", cmd) - return execute(params, supplemental) + return execute(params, supplemental, config.UsePwsh7) } // File executes a PowerShell script file. @@ -98,7 +102,7 @@ func File(path string, args []string, supplemental []string, config *PSConfig) ( params := append(config.Params, "-File", path) params = append(params, args...) - return execute(params, supplemental) + return execute(params, supplemental, config.UsePwsh7) } // Version gathers powershell version information from the host, returns an error if version information cannot be obtained.