Pergunta

How can I mimic this command in PowerShell using Start-Process?

my.exe < my.inp 2>&1 | tee my.log

I've tried using PS 3.0 extended output redirection as suggested here, however the log file contains only information about the process.

$app = Start-Process -PassThru -FilePath my.exe -RedirectStandardInput my.inp 2>&1 | Tee-Object my.log
Wait-Process $app.Id
Foi útil?

Solução 2

End up going with .NET objects.

$psi = New-Object System.Diagnostics.ProcessStartInfo 
$psi.CreateNoWindow = $true
$psi.RedirectStandardError = $true 
$psi.UseShellExecute = $false 
$psi.FileName = $EXE
$psi.RedirectStandardInput = $true
$psi.WorkingDirectory = $PWD

$process = New-Object System.Diagnostics.Process 
$process.StartInfo = $psi 
[void]$process.Start()

do
{
    $line = $process.StandardError.ReadLine()
    $line | Tee-Object "my.log" -Append
}
while (!$process.HasExited)    

$line = $process.StandardError.ReadToEnd()
$line | Tee-Object "my.log" -Append    

$process.Close();   

Outras dicas

Don't use -PassThru as that will output a Process object along with the other output from your exe. Also, all that output is being assigned to $app (not just the Process object). And all the output will get display in a separate window which flashes up while my.exe is running and then disappears. See if this gets you going:

Start-Process my.exe -NoNewWindow -Wait -RedirectStandardInput my.inp `
                     -RedirectStandardOutput foo.log `
                     -RedirectStandardError fooerr.log
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top