如何从 PowerShell 向控制台应用程序发送输入
How to send input to console application from PowerShell
在使用 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#>
谢谢!
我的首选解决方案:
$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()) { }
以下也适用于 non-console 应用程序,但在 non-interactive 上下文中可能无法正常工作:
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~")
在使用 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#>
谢谢!
我的首选解决方案:
$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()) { }
以下也适用于 non-console 应用程序,但在 non-interactive 上下文中可能无法正常工作:
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~")