fleet/orbit/pkg/scripts/exec_windows.go

31 lines
986 B
Go
Raw Normal View History

//go:build windows
package scripts
import (
"context"
"os/exec"
"path/filepath"
)
func execCmd(ctx context.Context, scriptPath string) (output []byte, exitCode int, err error) {
// initialize to -1 in case the process never starts
exitCode = -1
// for Windows, we execute the file with powershell.
2024-01-23 16:43:31 +00:00
cmd := exec.CommandContext(ctx, "powershell", "-MTA", "-ExecutionPolicy", "Bypass", "-File", scriptPath)
cmd.Dir = filepath.Dir(scriptPath)
output, err = cmd.CombinedOutput()
if cmd.ProcessState != nil {
// The windows exit code is a 32-bit unsigned integer, but the
// interpreter treats it like a signed integer. When a process
// is killed, it returns 0xFFFFFFFF (interpreted as -1). We
// convert the integer to an signed 32-bit integer to flip it
// to a -1 to match our expectations, and fit in our db column.
//
// https://en.wikipedia.org/wiki/Exit_status#Windows
exitCode = int(int32(cmd.ProcessState.ExitCode()))
}
return output, exitCode, err
}