1

I have a .exe file which when called from the Windows Command prompt, first connects to a server and then requests for user input as a confirmation. Something like below:

C:\SomePath\AnotherFolder>TheApplication.exe download
Logging in to Vault server myserver.app.cosmos...
Logged in to the Vault server

Downloading will overwrite existing files.
Download scripts for context 'COSMOS' to folder:
 C:\Work\MyFolder\TEST?
(y/n): n  <--- n was my input

Press any key to exit... <--- here i pressed y

I want to automate this process.

I tried the below line from the Windows command prompt:

 echo n|TheApplication.exe download

I even tried :

 (echo n && echo y)|TheApplication.exe download  <-- the y for the last user input request to close the app.

But in both cases its throwing the below error:

Unhandled Exception: System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
   at System.Console.ReadKey(Boolean intercept)
   at System.Console.ReadKey()
   at TheApplication.Program.Main(String[] args)

**disclaimer: TheApplication.exe is a third party executable. I cannot change anything inside it.

PS: If needed I can run this from a powershell console if its not possible from windows command prompt or I can also include the whole thing inside a batch script.

But any how I need to automate this.

SunainaDG
  • 109
  • 1
  • 14
  • Yep its a .net app. And as far as my knowledge goes it runs on .Net Framework 4.5.2 – SunainaDG Jul 11 '20 at 11:54
  • 1
    You'll have to get the app vendor to help you change `Console.ReadKey()` to `Console.Read()` – Gerhard Jul 11 '20 at 11:55
  • 1
    Read the documentation for your .EXE. When you're lucky, there are some command line parameters (aka switches) to replace the keyboard input. – Stephan Jul 11 '20 at 11:59
  • see if your app has some sort of help section built in. `TheApplication.exe /?` or `-h` or `--help` etc. to see if there might be some parameters for defaults maybe, as mentioned by @Stephan – Gerhard Jul 11 '20 at 12:02
  • @GerhardBarnard so no way this can be done using a batch script or powershell or anything else? – SunainaDG Jul 11 '20 at 12:35
  • Dis you check if the app has any help yet? – Gerhard Jul 11 '20 at 12:44
  • Yep, No luck there – SunainaDG Jul 11 '20 at 13:11
  • You'll have to ask the vendor then unfortunately. – Gerhard Jul 11 '20 at 15:27
  • If you are using CMD, you can use the < operator to read a file as keyboard input. Example here: https://stackoverflow.com/questions/17020106/how-to-make-an-exe-application-read-input-from-batch-file-instead-of-typing-it I can't find a way to do this in powershell though. – Daniel Kling Apr 30 '21 at 19:10

1 Answers1

0

If you can use PowerShell...

autoinput.ps1

Add-Type -AssemblyName System.Windows.Forms
Start-Sleep -m 1000
[System.Windows.Forms.SendKeys]::SendWait("ny")

The command is:

powershell .\autoinput.ps1 & .\TheApplication.exe
etsuhisa
  • 1,698
  • 1
  • 5
  • 7