Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

Start Process and read StandardOutput

I am looking for a way to start a process in a new console window or the same window and catch its output, I can open process in new window using:

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

This will open new window witch I can interact with but I cannot redirect output for it (output I mean whole interaction with window like key press and messages)

So I thought I can try with:

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();
}

Now here I get get output but without interaction, so I need some sort of option or procedure to start this process in same or different console and catch my interaction with it to file.

It is important as application sometimes asks for press any key and if Ill launch it in background it will never ask it because this app measures console window and checks id output will fit?

Is such thing possible?

Upvotes: 1

Views: 2436

Answers (3)

JRV
JRV

Reputation: 89

No matter how you do this it will be unreliable in an active system:

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
}

Upvotes: 0

Wojciech Szabowicz
Wojciech Szabowicz

Reputation: 4198

As I was having big problem with this kind of interactivity in powershell, I mean run console app and press key, I finally got it right and I am shearing my solution.

Press button to continue function:

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

Start process, log whole transaction and press to continue:

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
}

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

You should be able to do

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

or

myprocess.StandardInput.Write(" ");

Upvotes: 1

Related Questions