1

I'm writing a Powershell script which calls an external program like this:

& $toolLocation $toolParameters

The program - which is a console application - runs fine, but at the end it waits for the user to press enter to finish and exit. What I would like to achieve is that the script is not stopping at this point waiting for user input, but continues automatically. Is this possible? If yes, how?

Thanks in advance.

5
  • 1
    Remove the code from the tool that waits for user input. Commented Jun 18, 2014 at 12:56
  • I am not getting it compeletly. the script ENDS and u press any button to exit. but it shall continue after it ended? what do you expect it to do ? Commented Jun 18, 2014 at 12:56
  • @RayofCommand: Nope, the external program waits for enter to finish. c# console app with ReadLine() at the end. Commented Jun 18, 2014 at 13:00
  • @TrevorSullivan: Nice try, but it is developed by third party. Commented Jun 18, 2014 at 13:05
  • 3
    File it as a bug w/ the developer of the app to remove that ReadLine(). App behavior like that is incredibly annoying even if you're not trying to wrap a script around it. Commented Jun 18, 2014 at 13:10

2 Answers 2

1

Whatever the program, you can use wscript.shell to utilise the SendKeys method.

I have tested on the following:

$Wscript = New-Object -com wscript.shell
Start-Process -FilePath cmd.exe -Argumentlist "/c pause" ; Start-sleep -seconds 1; $wscript.SendKeys("{Enter}")

This has spawned me a cmd.exe with pause prompt which I then 'closed' with SendKeys. It of course depends on the window behaviour - the SendKeys sends the keys only to the active window, and if the application you are calling is not active, then there are couple of tricks...

Hope that helps

Sign up to request clarification or add additional context in comments.

2 Comments

Almost perfect, but the powershell script does not wait for the program to finish. It just lunches it then runs the rest and finishes. Is it possible to start the process synchronously?
You have to help yourself with that, unfortunately I cannot go further without knowing the application in detail. If the application stops and waits for user input to exit the system, and leaves no trace (like lets say, a file generated) the only way then is to extend start-sleep to the time where application is expected to finish. If the application does not react to the input, you could also try wrapping this call in "while" function, "trying" to close it every 5 secs with desired key. This would be a little more advanced however should point you in the right direction. Give it a try :-)
0

A cleaner solution, without dependencies, would be to output to the "Enter" key to the host, and pipe it to the applpication. In this case, you could execute your console application like this:

Write-Host "" | & $toolLocation $toolParameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.