3

Using Powershell I am starting some executable using invoke-expression like:

Invoke-Expression "c:\exec.exe"

now my problem is this exec is showing something like "pause and press any key to continue".

I tried:

Function Run-Tool
{
    Invoke-Expression "c:\exec.exe"
    $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

But no luck.

My question is can I ignore that message some sort of suppression or is there a way to monitor output and simulate pressing any key?

JensG
  • 13,148
  • 4
  • 45
  • 55
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • 1
    Does the executable not have a `/quiet` switch? – gvee Feb 01 '17 at 14:31
  • May I ask what EXE that is? Is it a commonly available program maybe? – JensG Feb 01 '17 at 14:35
  • No it is an 3rd part app that I am trying to automatize. – Wojciech Szabowicz Feb 01 '17 at 14:36
  • Well, if it is a secret then I can only give the general advise to run that program with arguments like `/?` or `-?` or `-h` or `--help`, which are commonly used args for getting detailed help about possible command line arguments. With some luck you'll find the info you are looking for – JensG Feb 01 '17 at 14:38
  • ``"`n"|& C:\exec.exe`` might work – Mathias R. Jessen Feb 01 '17 at 15:33
  • 1
    @MathiasR.Jessen: Why not make it an answer? Or is that only a raw, untested guess? – JensG Feb 02 '17 at 10:16
  • @JensG It only works under certain circumstances (ie. not with `Console.ReadKey()`), OP doesn't provide enough detail to determine whether it would work in his case, but it's probably worth a try – Mathias R. Jessen Feb 02 '17 at 11:05

3 Answers3

8

If exec.exe is a regular console app expecting a newline a la:

Console.Write("Press any key to exit...");
Console.ReadLine();

Then you could just pipe a newline to it:

"`n" |& C:\exec.exe
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
2

you can redirect the enter key from powershell to the program by using processstartinfo and process :

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "C:\yourexe.exe"; 
$psi.UseShellExecute = $false; 
$psi.RedirectStandardInput = $true;

$p = [System.Diagnostics.Process]::Start($psi);

Start-Sleep -s 2 # if your exe needs time to give output

$p.StandardInput.WriteLine("`n");
ClumsyPuffin
  • 3,909
  • 1
  • 17
  • 17
0

Using $host.UI.RawUI.ReadKey will not do what you are after - the ReadKey function waits for input and returns info on the key that was pressed. So instead of sending input, it is waiting for input.

This answer is related to your situation - perhaps you can use the method suggested there.

Community
  • 1
  • 1
Daniel Brixen
  • 1,633
  • 13
  • 20