gpt4 book ai didi

powershell - 使用 Powershell Out-Host 时保留颜色

转载 作者:行者123 更新时间:2023-12-03 18:21:19 25 4
gpt4 key购买 nike

我有一个命令行可执行文件,可以将彩色文本打印为标准输出。当我做以下...
my_executable.exe | Out-Host
...我发现 Out-Host 删除了所有彩色输出。当我想显示程序的彩色输出而不通过管道发送它时,这是一个问题。

有没有另一种方法可以让我显示控制台程序/批处理文件的彩色输出而不通过管道发送?

编辑:

换句话说,我想要的是:

Output with coloring

但是 Out-Host 导致了这个:

Output without coloring

编辑2:

这就是为什么我不能只调用 my_executable.exe 的原因:

我在 .ps1 文件中有一个类似于以下内容的脚本

param($someValue)

# do some things
.\my_executable.exe | Out-Host
# do some more things
return 1

现在,当我执行以下操作时:
$result = .\my-script.ps1

$result 为 1。如果我不使用 Out-Host,$result 将是 my_executable.exe 的输出,最后为 1。 Out-Host 允许我显示 my_executable.exe 的输出而不将其返回给调用者。我想要的是一种显示 my_executable.exe 的彩色输出而不将其返回给调用者的方法。

最佳答案

$exe = ".\my_executable.exe"
$args = ""
$process = Start-Process $exe $args -NoNewWindow -Wait -ErrorAction Stop -PassThru
if ($process.ExitCode -ne 0) {
throw "FAILED: $exe $args"
}

关于powershell - 使用 Powershell Out-Host 时保留颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11234062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com