PowerShell 中的输出重定向仍带有颜色

发布于 2024-08-31 11:49:11 字数 1208 浏览 4 评论 0原文

假设我像这样运行 msbuild:

function Clean-Sln {
    param($sln)
    MSBuild.exe $sln /target:Clean
}
Clean-Sln c:\temp\SO.sln

在 Posh 控制台中,输出是彩色的。这非常方便 - 只需观察输出即可发现颜色。例如,不重要的消息是灰色的。

问题

我想添加将其重定向到这样的位置的功能(简化示例):

function Clean-Sln {
    param($sln)
    MSBuild.exe $sln /target:Clean | Redirect-AccordingToRedirectionVariable
}
$global:Redirection = 'Console'
Clean-Sln c:\temp\SO.sln
$global:Redirection = 'TempFile'
Clean-Sln c:\temp\Another.sln
  • 如果我使用“Console”,则 cmdlet/函数 Redirect-AccordingToRedirectionVariable 应输出msbuild 消息的颜色与未通过管道传输输出的方式相同。换句话说 - 它应该保持输出原样。
  • 如果我使用“TempFile”,Redirect-AccordingToRedirectionVariable 会将输出存储在临时文件中。

有可能吗?我想不是:| 或者您对如何实现目标有什么建议吗?

可能的解决方案:

if ($Redirection -eq 'Console) {
  MSBuild.exe $sln /target:Clean | Redirect-AccordingToRedirectionVariable
} else {
  MSBuild.exe $sln /target:Clean | Out-File c:\temp.txt  
}

但是,如果您想象可能有很多 msbuild 调用,那么这并不理想。

不要害羞地告诉我任何如何应对它的新建议;)


也欢迎任何有关重定向/着色/输出的背景信息。
(问题不是 msbuild 特有的,问题涉及任何写入彩色输出的应用程序)

Suppose I run msbuild like this:

function Clean-Sln {
    param($sln)
    MSBuild.exe $sln /target:Clean
}
Clean-Sln c:\temp\SO.sln

In Posh console the output is in colors. That's pretty handy - you spot colors just by watching the output. And e.g. not important messages are grey.

Question

I'd like to add ability to redirect it somewhere like this (simplified example):

function Clean-Sln {
    param($sln)
    MSBuild.exe $sln /target:Clean | Redirect-AccordingToRedirectionVariable
}
$global:Redirection = 'Console'
Clean-Sln c:\temp\SO.sln
$global:Redirection = 'TempFile'
Clean-Sln c:\temp\Another.sln
  • If I use 'Console', the cmdlet/function Redirect-AccordingToRedirectionVariable should output the msbuild messages with colors the same way as the output was not piped. In other words - it should leave the output as it is.
  • If I use 'TempFile', Redirect-AccordingToRedirectionVariable will store the output in a temp file.

Is it even possible? I guess it is not :|
Or do you have any advice how to achieve the goal?

Possible solution:

if ($Redirection -eq 'Console) {
  MSBuild.exe $sln /target:Clean | Redirect-AccordingToRedirectionVariable
} else {
  MSBuild.exe $sln /target:Clean | Out-File c:\temp.txt  
}

But if you imagine there can be many many msbuild calls, it's not ideal.

Don't be shy to tell me any new suggestion how to cope with it ;)


Any background info about redirections/coloring/outpu is welcome as well.

(The problem is not msbuild specific, the problem touches any application that writes colored output)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尴尬癌患者 2024-09-07 11:49:11

是的,我会避免管道彩色输出。那时,AFAICT,所有颜色信息都丢失了。
我建议在 MSBuild 上使用 /filelogger 和 /noconsolelogger 参数,例如:

function Invoke-MSBuild($project, [string[]]$targets, [switch]$logToFile) {
    $OFS = ';'
    $targetArg = if ($targets) {"/t:$targets"} else {''}
    if ($logToFile) {
        msbuild.exe $project $targetArg  /filelogger /noconsolelogger
    }
    else {
        msbuild.exe $project $targetArg 
    }
}

或者您可以做一些更简单的事情,如下所示:

function Invoke-MSBuild($project, [string[]]$targets, $logFile) {
    $OFS = ';'
    $targetArg = if ($targets) {"/t:$targets"} else {''}
    if ($logFile) {
        msbuild.exe $project $targetArg > $logFile
    }
    else {
        msbuild.exe $project $targetArg 
    }
}

Yeah I would avoid piping colored output. At that point, AFAICT, all color info is lost.
I would recommend using the /filelogger and /noconsolelogger parameters on MSBuild e.g.:

function Invoke-MSBuild($project, [string[]]$targets, [switch]$logToFile) {
    $OFS = ';'
    $targetArg = if ($targets) {"/t:$targets"} else {''}
    if ($logToFile) {
        msbuild.exe $project $targetArg  /filelogger /noconsolelogger
    }
    else {
        msbuild.exe $project $targetArg 
    }
}

or you could do something even simpler like this:

function Invoke-MSBuild($project, [string[]]$targets, $logFile) {
    $OFS = ';'
    $targetArg = if ($targets) {"/t:$targets"} else {''}
    if ($logFile) {
        msbuild.exe $project $targetArg > $logFile
    }
    else {
        msbuild.exe $project $targetArg 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文