启动流程并阅读StandardOutput

时间:2017-02-03 09:52:58

标签: powershell

我正在寻找一种在新的控制台窗口或同一窗口中启动进程并捕获其输出的方法,我可以使用以下命令在新窗口中打开进程:

[Diagnostics.Process]::Start("C:\test.exe","-verbose -page") 

这将打开我可以与之交互的新窗口,但我无法为其重定向输出(输出我的意思是与窗口的整个交互,如按键和消息)

所以我想我可以尝试:

Start-Transcript -path "C:\test.txt" -append

$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = "C:\test.exe"
$ps.StartInfo.Arguments = " -verbose -page"
$ps.StartInfo.RedirectStandardOutput = $True
$ps.StartInfo.UseShellExecute = $false
$ps.start()

while ( ! $ps.HasExited ) {
    write-host = $ps.StandardOutput.ReadToEnd();
}

现在我在这里得到输出但没有交互,所以我需要某种选项或程序来在相同或不同的控制台中启动这个过程,并抓住我与它的交互来存档。

重要的是,应用程序有时会要求按任意键,如果我在后台启动它,它永远不会问它,因为这个应用程序测量控制台窗口并检查id输出是否合适?

这样的事情可能吗?

3 个答案:

答案 0 :(得分:1)

你应该能够做到

myprocess.StandardInput.WriteLine("some input");

myprocess.StandardInput.Write(" ");

答案 1 :(得分:0)

由于我在powershell中遇到这种交互性的大问题,我的意思是运行控制台应用程序并按键,我终于做对了,我正在剪切我的解决方案。

按按钮继续功能:

Function Press-Button
{
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.SendKeys]::SendWait('~');
}

启动流程,记录整个交易,然后按继续:

Function Run-Tool
{   
    Start-Transcript -path "C:\test.txt" -append

    $ps = new-object System.Diagnostics.Process
    $ps.StartInfo.Filename = "C:\test.exe"
    $ps.StartInfo.Arguments = " -verbose -page"
    $ps.StartInfo.RedirectStandardInput = $true;
    $ps.StartInfo.UseShellExecute = $false
    $ps.start()

    while ( ! $ps.HasExited ) 
    {
        Start-Sleep -s 5
        write-host "I will press button now..."
        Press-Button        
    }

    Stop-Transcript
}

答案 2 :(得分:0)

无论你如何做到这一点,它在主动系统中都是不可靠的:

Function Run-Tool {
    Start-Transcript -path "C:\test.txt" -append
    Add-Type -AssemblyName System.Windows.Forms

    $ps = new-object System.Diagnostics.Process
    $ps.StartInfo.Filename = "C:\test.exe"
    $ps.StartInfo.Arguments = " -verbose -page"
    $ps.StartInfo.RedirectStandardInput = $true;
    $ps.StartInfo.UseShellExecute = $false
    $ps.start() 

    do{ 
        Start-Sleep -s 5
        write-host "I will press button now..."
        [System.Windows.Forms.SendKeys]::SendWait('~');
    }
    until($ps.HasExited)
    Stop-Transcript
}
相关问题