1

I am writing a batch file to automatically run some EXE and exit the message comes in console

"Press any key to continue .. "

The batch file contains following code:

D:\\Tethys\\SVN\\build\\vs2010\\ReleaseWin32\\bin\\TethysDatabaseUtility.exe install       D:\Tethys\DatabaseInstaller\DatabaseInstaller\RGTestingUtility.cab "" NDI-LAP-262\SQL2008R2 TestDB-Neeraj N sa Brick@123 /c h:
D:\\Tethys\\SVN\\build\\vs2010\\ReleaseWin32\\bin\\TethysDatabaseUtility.exe install D:\Tethys\DatabaseInstaller\DatabaseInstaller\RGTestingUtilityTestCases.cab "" NDI-LAP-262\SQL2008R2 TestDB-Neeraj N sa Brick@123 
D:\\Tethys\\SVN\\build\\vs2010\\ReleaseWin32\\bin\\TethysDatabaseUtility.exe install D:\Tethys\DatabaseInstaller\DatabaseInstaller\RGTestingUtilityBaseLineData.cab "" NDI-LAP-262\SQL2008R2 TestDB-Neeraj N sa Brick@123 

The exe using in bat file is a console application and code written

installer.run();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();

the above code is requirement so i do not change the code,since that console exe is call some other application also.

Please help to exit installer.

enter image description here

After adding echo(|

I have received following error on console.

enter image description here

Neeraj Dubey
  • 4,401
  • 8
  • 30
  • 49
  • 2
    Have you tried `echo(|myEXEFile.exe`? – MC ND Sep 17 '14 at 14:02
  • I have update batch file code please suggests how to write `echo(|myEXEFile.exe` – Neeraj Dubey Sep 17 '14 at 14:20
  • `echo(|"D:\Tethys\S...\TethysDatabaseUtility.exe" install ...` The idea is to pipe the CRLF from the `echo` command as the standard input to the console application. Depending on how `Console.ReadKey()` is implemented, it can work or not. – MC ND Sep 17 '14 at 14:31
  • the message you write in your question does not match the screenshot or the c# code. – ths Sep 17 '14 at 17:42
  • ReadKey() will not work with stdin. You need to use Console.Read() to read from stdin. – Keith Hill Sep 17 '14 at 19:50
  • @MC ND i have tried your code and received an error updated on question. – Neeraj Dubey Sep 18 '14 at 06:18
  • 1
    Unfortunately piping does not work as `ReadKey` tries to get the handle to the console input buffer and it has been redirected. You need to modify you application code, adding an aditional parameter to handle this cases, or changing the problematic code to something like `if (!Console.IsInputRedirected) Console.ReadKey()` (sorry, .Net 4.5) or add code to handle the error. Or you can use something to send the keystroke to the application, as Keith Hill answers, or [here](http://stackoverflow.com/a/23625920/2861476) i posted a little c code (after the edit). Tested against ReadKey and works – MC ND Sep 18 '14 at 07:12

1 Answers1

0

You can do this with a background job (assuming you're on V2 or higher). The trick is to use the Windows Forms SendKeys.SendWait() method to simulate a key press on the PowerShell window that is waiting for key press. You'll have to either use a delay or you could modify the job script to check for something that indicates the installer is finished. Here's the code:

Start-Job {param($id) Add-Type -Assembly System.Windows.Forms
                      $hwnd = (Get-Process -id $id).MainWindowHandle
                      Start-Sleep -Seconds 10
                      Set-ForegroundWindow $hwnd
                      [Windows.Forms.SendKeys]::SendWait('{ENTER}') 
           } -ArgumentList $pid > $null

I tested this with a simple console app I wrote. BTW this uses a command from PSCX (PowerShell Community Extensions - http://pscx.codeplex.com) called Set-ForegroundWindow. The implementation is pretty simple if you don't want to take a dependency on PSCX.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369