从powershell运行exe并将流输出到同一控制台



我有一个可以从PowerShell调用的应用程序。它使用一个参数作为一组指令。一切都很好,但我希望将消息保持在同一个窗口中,因为当调用它时,它会打开一个新窗口,而原始的ps控制台将不会得到任何输出。

我已经尝试了以下代码。它打开了一个新窗口,在新窗口上我可以看到输出文本。此外,文件out.txt已生成,但未填充。

$execFile = “C:Program FilesDBBestDatabase Compare SuiteDBBest.DataSuite.Console.exe”
$params = "C:SchemaSync1.xml"

Start-Process -FilePath $execFile -NoNewWindow -wait -ArgumentList $params -RedirectStandardError error.txt -RedirectStandardOutput out.txt -ErrorAction Continue

我也尝试过调用这个Csharp类,但我仍然打开了一个新窗口,并且从未在原始的ps控制台中获得输出。

$psi = New-object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
$psi.FileName = 'C:Program FilesDBBestDatabase Compare SuiteDBBest.DataSuite.Console.exe'
$psi.Arguments = "C:SchemaSync1.xml"
$psi.LoadUserProfile = $true
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $psi
[void]$process.Start()
$output = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
$output

我想要输出的原因是在构建代理上自动化这个过程,我可以捕捉到任何错误。

感谢

如果它像这样单独打开一个新控制台,你可能不会在powershell中看到那里的数据,除非它专门将数据返回到stdout。这里还有一些你可以尝试的东西:

您可以尝试将所有输出流发送到标准输出:

Start-Process $execFile $params *>&1

您可以尝试将其作为powershell作业运行——作业对象分别捕获每个输出流,并捕获子进程的输出:

$job = Start-Job -Name Foo -ScriptBlock {Start-Process $execFile $params}
Wait-Job $job
# check for any child jobs (and check these for child jobs too, depending on your process):
$job.ChildJobs.Name
# check job or child job(s) for any output. This just formats warning and errors nicely:
[pscustomobject] @{
Information = $job.Information
Warning     = $job.Warning.Message
Error       = $job.Error.Exception.Message
Output      = $job.Output
} | 
Format-List

您可能更幸运地查看DBBest.DataSuite.Console.exe.config文件以获得更好的日志记录选项,或者可以使用某种-verbose标志运行程序。

最新更新