【问题标题】:Start Process and read StandardOutput启动 Process 并读取 StandardOutput
【发布时间】:2017-02-03 20:15:27
【问题描述】:

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

[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 输出是否适合?

这样的事情可能吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    你应该可以的

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

    myprocess.StandardInput.Write(" ");
    

    【讨论】:

      【解决方案2】:

      由于我对 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
      }
      

      【讨论】:

        【解决方案3】:

        无论你怎么做,它在一个活跃的系统中都是不可靠的:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 2014-05-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多