简体   繁体   中英

How to ignore pause - press any key - in Powershell while executing file

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?

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

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM