Redirect Input (and output) on an external process.

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 3 years, 4 months ago.

  • Author
    Posts
  • #14262

    Troy Helms
    Participant

    A project I have been working on calls an external executable that I have little control over. It uses StandardOutput and StandardError which I can capture just fine from other similar things I've done. Unfortunately the program doesn't take commandline input. It prompts. That's where I can't figure out how to read what it's prompting for, how to send it what it's prompting for, etc.

    When you run the problem, it asks: "Please type in the path to the CSV files."
    Once you do this and press enter, it completes, then prompts: "Completed, Please press Enter."
    Then it exists.

    I can't figure out how to feed it the first item, then wait for the "completed prompt" to send it an "Enter" so it will exit and allow me to capture the StandardOutput and StandardError to log it and move on with my script.

    Any ideas? I have been working with a chunk of code I have used in the past that looks like this:

    $ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo 
    $ProcessInfo.FileName = "ping.exe" 
    $ProcessInfo.RedirectStandardError = $true 
    $ProcessInfo.RedirectStandardOutput = $true 
    $ProcessInfo.UseShellExecute = $false 
    $ProcessInfo.Arguments = "localhost -t" 
    $Process = New-Object System.Diagnostics.Process 
    $Process.StartInfo = $ProcessInfo 
    $Process.Start() | Out-Null 
    $Process.WaitForExit() 
    $output = $Process.StandardOutput.ReadToEnd() 
    $output 

    That piece only works with a process that runs, then exits, but doesn't require additional input. Has anyone worked with something like this and have any clues on how to proceed? Or am I stuck? Thanks!

  • #14264

    Don Jones
    Keymaster

    If it supports StdIn for input, you can try piping the path to it (e.g., "path" > executable.exe). Otherwise, PowerShell wouldn't really have a way to respond. Keep in mind that, when your external program is executing, it's doing so under Cmd.exe, not under PowerShell. You're "out" of PowerShell at that time.

    Now, that said, a Process object does have a StandardInput property (see http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process). Possibly you could use that – again, IF the application accepts input via StdIn.

You must be logged in to reply to this topic.