【问题标题】:Output from function to console, preserving color从函数输出到控制台,保留颜色
【发布时间】:2013-06-09 19:49:59
【问题描述】:

我在一个函数内部从 powershell 运行 msbuild:

function Run-MSBuild()
{
    msbuild.exe (lots of params)
}

Run-MSBuild

效果很好,我在控制台上看到了彩色的 MSBuild 输出。现在,我想知道花了多长时间:

$time = Measure-Command { Run-MSBuild }

有效,但我再也看不到 MSBuild 输出了。

尝试1:管道到Write-Host

function Run-MSBuild()
{
    msbuild.exe (lots of params) | Write-Host
}

结果 1:我在控制台上看到 MSBuild 输出,但颜色丢失。

尝试2:替换Measure-Command

$timer = [diagnostics.stopwatch]::startnew()
Run-MSBuild
$time = $timer.Elapsed

结果 2:有效,但有点丑。

有什么方法可以做到这一点?

【问题讨论】:

    标签: powershell console


    【解决方案1】:

    在函数中包含定时命令?

    function Run-MSBuild
    {
        $start = Get-Date
        msbuild.exe $args
        $End = Get-Date
    
        $End-$Start
    }
    

    【讨论】:

    • 如果 Write-Host 不保留颜色,那么这似乎是唯一明智的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多