简体   繁体   English

从Tee-Object Powershell去除颜色

[英]Remove Color From Tee-Object Powershell

Start-Process "powershell" -ArgumentList "-noexit -executionpolicy bypass -windowstyle minimized -command `"&{Invoke-Expression `'.\$exe $Arguments`' | Tee-Object `'$Logs`'}`""

This command works, however the .exe I am running has color text, like: 此命令有效,但是我正在运行的.exe具有彩色文本,例如:

[0m

Which makes it hard to parse the resulting .log file. 这使得很难解析生成的.log文件。

Is there way to Tee-Object to a file, and remove the color output? 有没有办法将Tee-Object放到文件中并删除颜色输出?

Also, is there a way to do so, while keeping the color on the console, as the console displays the same characters (no color). 另外,有一种方法可以在控制台上保留颜色的同时,使控制台显示相同的字符(无颜色)。

I have been searching up and down, re-wrote it a hundred different ways, and I can't seem to find a way to remove it. 我一直在上下搜索,以一百种不同的方式重写了它,但似乎找不到删除它的方法。

Also, if there is cleaner way to write the launch besides invoke-expression | Tee-Object 另外,如果有除invoke-expression | Tee-Object之外更干净的方法来编写启动, invoke-expression | Tee-Object

It seems to be the only one that works for me. 它似乎是唯一对我有用的。

function Tee-ObjectNoColor {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [string]$InputObject,

        [Parameter(Position=1, Mandatory=$true)]
        [string]$FilePath
    )

  process{
        $InputObject = $InputObject -replace '\\[\d+(;\d+)?m'
        $InputObject | Out-File $FilePath -Append
        $InputObject | Out-Host
         }
}

This is how I did it. 这就是我做的。 It removes all color symbols, clean on both screen and log. 它将删除所有颜色符号,在屏幕和日志上均进行清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM