如何从PowerShell向控制台应用程序发送输入

如何从PowerShell向控制台应用程序发送输入,powershell,adb,console-application,Powershell,Adb,Console Application,在使用powershell脚本启动adb shell控制台应用程序之后,我想自动将用户输入到此控制台应用程序 adb shell "/usr/bin/console_app" 有没有办法让powershell脚本同时输入这些控制台输入,而不让它等待用户输入 比如: adb shell "/usr/bin/console_app" sleep -s 5 <# wait for app to start#> 1 <# user input

在使用powershell脚本启动adb shell控制台应用程序之后,我想自动将用户输入到此控制台应用程序

adb shell "/usr/bin/console_app"
有没有办法让powershell脚本同时输入这些控制台输入,而不让它等待用户输入

比如:

adb shell "/usr/bin/console_app"
sleep -s 5 <# wait for app to start#>
1 <# user input for menu selection#>
adb shell”/usr/bin/console\u应用程序
睡眠-s 5
1.
谢谢

我的首选解决方案:

$startInfo = New-Object 'System.Diagnostics.ProcessStartInfo' -Property @{
    FileName = "adb"
    Arguments = "shell", "/usr/bin/console_app"
    UseShellExecute = $false
    RedirectStandardInput = $true
}
$process = [System.Diagnostics.Process]::Start($startInfo)
Start-Sleep -Seconds 5
$process.StandardInput.WriteLine("1")
您还可以尝试通过读取输出来等待应用程序完成启动:

# set this on the ProcessStartInfo:
RedirectStandardOutput = $true

# wait while application returns stdout
while ($process.StandardOutput.ReadLine()) { }
以下内容也适用于非控制台应用程序,但在非交互式上下文中可能无法正常工作:

Add-Type -AssemblyName 'System.Windows.Forms', 'Microsoft.VisualBasic'
$id = (Start-Process "adb" -ArgumentList "shell", "/usr/bin/console_app" -PassThru).Id
Start-Sleep -Seconds 5
[Microsoft.VisualBasic.Interaction]::AppActivate($id)
[System.Windows.Forms.SendKeys]::SendWait("1~")

很好,第一个建议的解决方案在我的.ps1脚本中起作用。我也会尝试第二种解决方案。谢谢你的帮助!后续问题:如果应用程序已经启动以取代简单的启动睡眠,是否有办法从adb获得反馈?@T.Richter您可以尝试的一件事是从标准输出读取数据。我把它添加到我的答案中,这应该让你开始。