Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows - Process keeps running when stdin is set #219

Open
powellnorma opened this issue Dec 30, 2023 · 0 comments
Open

Windows - Process keeps running when stdin is set #219

powellnorma opened this issue Dec 30, 2023 · 0 comments

Comments

@powellnorma
Copy link

The following works fine:

func runPsCmd(psCmd string) {
	pthPs := `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`

	cmd := exec.Command(pthPs, "-Command", psCmd)

	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin

	err := cmd.Start()
	if err != nil {
		panic(err)
	}

	err = cmd.Wait()
	if err != nil {
		exitError, ok := err.(*exec.ExitError)
		if ok {
			os.Exit(exitError.ExitCode())
		} else {
			panic(err)
		}
	}
	os.Exit(0)
}

func main() {
	runPsCmd("echo myTest")
}

However when doing the same but with:

func runPsCmd(s ssh.Session, psCmd string) {
	// [..]
	cmd.Stderr = s
	cmd.Stdout = s
	cmd.Stdin = s
	// [..]
}

cmd.Wait() blocks until I pressed Enter once. When I don't assign cmd.Stdin at all, it works as expected, but some programs may require user input. What could this by caused by? And how can this be fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant