我在网上搜索了那个问题并登陆了服务器故障:
我可以向屏幕会话中运行的活动进程的 STDIN 发送一些文本吗?
似乎在 Linux 下实现这一点非常容易。但我需要它作为 Win32 命令提示符。
背景:我有一个轮询 STDIN 的应用程序,如果我按下该x键,应用程序将终止。现在,我想做一些自动化测试,测试应用程序然后关闭它。
注意:仅终止进程不是一种选择,因为我目前正在调查关闭应用程序期间出现的问题。
我在网上搜索了那个问题并登陆了服务器故障:
我可以向屏幕会话中运行的活动进程的 STDIN 发送一些文本吗?
似乎在 Linux 下实现这一点非常容易。但我需要它作为 Win32 命令提示符。
背景:我有一个轮询 STDIN 的应用程序,如果我按下该x键,应用程序将终止。现在,我想做一些自动化测试,测试应用程序然后关闭它。
注意:仅终止进程不是一种选择,因为我目前正在调查关闭应用程序期间出现的问题。
.NET 框架的Process和ProcessStartInfo类可用于创建和控制进程。由于 Windows PowerShell 可用于实例化 .NET 对象,因此可以在 PowerShell 中控制进程的几乎所有方面。
以下是如何将dir
命令发送到cmd.exe
进程(确保将其包装在 .ps1 文件中,然后执行脚本):
$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