【问题标题】:Run exe from powershell and output the stream to the same console从 powershell 运行 exe 并将流输出到同一个控制台
【发布时间】:2021-06-23 03:29:07
【问题描述】:

我有一个可以从 PowerShell 调用的应用程序。它使用一个参数作为一组指令。一切正常,但是我想将消息保留在同一个窗口中,因为调用它会打开一个新窗口,并且原始 ps 控制台不会得到任何输出。

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

    $execFile = “C:\Program Files\DBBest\Database Compare Suite\DBBest.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 Files\DBBest\Database Compare Suite\DBBest.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

我想要输出的原因是在构建代理上自动执行此过程,我可以捕获任何错误。

谢谢

【问题讨论】:

  • 你试过& $execfile $params吗?

标签: powershell cmd


【解决方案1】:

如果它像这样自行打开一个新控制台,您可能不会在 powershell 中看到那里的数据,除非它专门将该数据返回到标准输出。不过,您可以尝试以下其他方法:

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

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 标志可能会让您更幸运地运行程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多