Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I send some text to the STDIN of an active process under Windows?

Tags:

I searched the web for that question and landed on Server Fault:

Can I send some text to the STDIN of an active process running in a screen session?

Seems like it is ridiculously easy to achieve this under Linux. But I need it for a Win32 Command Prompt.

Background: I have an application that polls STDIN and if I press the x key, the application terminates. Now, I want to do some automated testing, test the application and then shut it down.

Note: Just killing the process is not an option since I'm currently investigating problems that arise during the shutdown of my application.

like image 780
eckes Avatar asked Apr 19 '13 06:04

eckes


People also ask

How do you take string input in PowerShell?

Description. The Read-Host cmdlet reads a line of input from the console (stdin). You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords.

How do you prompt a parameter in PowerShell?

You can use a mandatory parameter to prompt user input in the PowerShell script or function during the execution. Here is an example of function Name that asks for the user input when it is run. Copy function Name { param( [Parameter(Mandatory)] [string]$name ) Write-Output "Your name is $name." }

How do you wait for user input in PowerShell?

A: You can prompt for user input with PowerShell by using the Read-Host cmdlet. The Read-Host cmdlet reads a line of input from the PowerShell console. The –Prompt parameter enables you to display a string of text. PowerShell will append a colon to the end of the string.

How do I read a PowerShell script?

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.


1 Answers

.NET framework's Process and ProcessStartInfo classes can be used to create and control a process. Since Windows PowerShell can be used to instantiate .NET objects, the ability to control almost every aspect of a process is available from within PowerShell.

Here's how you can send the dir command to a cmd.exe process (make sure to wrap this in a .ps1 file and then execute the script):

$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 
like image 151
Abbas Avatar answered Sep 30 '22 19:09

Abbas