简体   繁体   中英

Send stdin to a process from windows command prompt

In Windows I have console programs that run in the background with the console hidden. Is there anyway to direct input to the programs console? I want to be able to do something like:

echo Y| *the_running_process_here*

to send Y to the process' stdin.

As far as I know this is not possible by basic cmd commands. PowerShell is more powerful though and can use the .NET framework from windows.

I found this PowerShell script which claims to pass text to an already opened cmd :

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "cmd.exe"; #process file
$psi.UseShellExecute = $false; #start the process from it's own executable file
$psi.RedirectStandardInput = $true; #enable the process to read from standard input

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

Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running

$p.StandardInput.WriteLine("dir"); #StandardInput property of the Process is a .NET StreamWriter object

Source: https://stackoverflow.com/a/16100200/10588376

You would have to safe this script with an .ps1 ending and run it.

A workaround to use this as a cmd command would be to make it accept arguments, so that you can run it with yourScript.ps1 procced_pid arguments for example.

The standard windows cmd is just too limited to fullfill such a task by its own.

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