Skip to content

Commit 23dfe73

Browse files
WinOps Teamcopybara-github
authored andcommitted
Add powershell 7 support for sessions
PiperOrigin-RevId: 784039547
1 parent 0e7e211 commit 23dfe73

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

powershell/powershell.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const (
4343
// PSConfig represents the command line parameters to use with invocations.
4444
type PSConfig struct {
4545
ErrAction ErrorAction // The ErrorAction that is set.
46-
Params []string // Additional parameters for calls to powershell.exe
46+
Params []string // Additional parameters for calls to powershell.exe.
47+
usePwsh7 bool // Use PowerShell 7 if true.
4748
}
4849

4950
var (

powershell/powershell_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package powershell
1919

2020
import (
2121
"errors"
22+
"os"
2223
"path/filepath"
2324
"testing"
2425

@@ -125,6 +126,40 @@ func TestNewSession(t *testing.T) {
125126
}
126127
}
127128

129+
func TestNewSessionPwsh7(t *testing.T) {
130+
// Stub out pwsh7 since it doesn't exist on Forge.
131+
powerShell7Exe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`)
132+
ps, err := NewSessionPwsh7()
133+
if err != nil {
134+
t.Fatalf("NewSessionPwsh7() failed: %v", err)
135+
}
136+
if err := ps.Close(); err != nil {
137+
t.Fatalf("Close() failed: %v", err)
138+
}
139+
}
140+
141+
func TestNewSessionNoPwsh7(t *testing.T) {
142+
ps, err := newSession(false)
143+
if err != nil {
144+
t.Fatalf("newSession() failed: %v", err)
145+
}
146+
if err := ps.Close(); err != nil {
147+
t.Fatalf("Close() failed: %v", err)
148+
}
149+
}
150+
151+
func TestNewSessionWithPwsh7(t *testing.T) {
152+
// Stub out pwsh7 since it doesn't exist on Forge.
153+
powerShell7Exe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`)
154+
ps, err := newSession(true)
155+
if err != nil {
156+
t.Fatalf("newSession() failed: %v", err)
157+
}
158+
if err := ps.Close(); err != nil {
159+
t.Fatalf("Close() failed: %v", err)
160+
}
161+
}
162+
128163
func TestExecute(t *testing.T) {
129164
ps, err := NewSession()
130165
if err != nil {

powershell/powershell_windows.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
powerShellCmd = powerShell
3535

3636
powerShellExe = filepath.Join(os.Getenv("SystemRoot"), `\System32\WindowsPowerShell\v1.0\powershell.exe`)
37+
powerShell7Exe = `C:\Program Files\PowerShell\7\pwsh.exe`
3738
)
3839

3940
// powerShell represents the OS command used to run a powerShell cmdlet on
@@ -121,7 +122,21 @@ type Session struct {
121122

122123
// NewSession creates and starts a new PowerShell session.
123124
func NewSession() (*Session, error) {
124-
cmd := exec.Command(powerShellExe, "-NoExit", "-NoProfile", "-Command", "-")
125+
return newSession(false)
126+
}
127+
128+
// NewSessionPwsh7 creates and starts a new PowerShell 7 session.
129+
func NewSessionPwsh7() (*Session, error) {
130+
return newSession(true)
131+
}
132+
133+
func newSession(usePwsh7 bool) (*Session, error) {
134+
exe := powerShellExe
135+
if usePwsh7 {
136+
exe = powerShell7Exe
137+
}
138+
139+
cmd := exec.Command(exe, "-NoExit", "-NoProfile", "-Command", "-")
125140
stdin, err := cmd.StdinPipe()
126141
if err != nil {
127142
return nil, fmt.Errorf("failed to open stdin pipe: %w", err)

0 commit comments

Comments
 (0)