当使用Get-Content命令时,Powershell终端输出的颜色不正确,为什么?

3
我有一个日志文件,其中包含了颜色编码,并且我想要使用 Powershell 来“跟踪尾部”查看日志。当我调用下面的脚本时,Powershell 会以其标准的蓝色背景启动,并且在日志文件中编码的颜色也会显示出来。
Get-Content -Path 'C:\myapp.out' -Wait

然而,我想要的是将Powershell终端的背景改为黑色,但保留日志文件中的颜色编码。因此,经过一些研究,我发现可以更改Powershell终端的某些方面,于是我的脚本变成了这样:

$host.ui.RawUI.WindowTitle = "Log Title"
$host.ui.RawUI.BackgroundColor = "black"

Clear-Host

Get-Content -Path 'C:\myapp.out' -Wait

Powershell终端的背景确实会变成黑色,但似乎Get-Content的输出仍然保留着标准Powershell的蓝色背景。因此,经过更多的研究,我了解到可以这样做:

$host.ui.RawUI.WindowTitle = "Log Title"
$host.ui.RawUI.BackgroundColor = "black"

Clear-Host

Get-Content -Path 'C:\myapp.out' -Wait  | ForEach { write-host $_ -BackgroundColor "black"}

现在,这确实有点改变了,但并不是使得myapp.out文件中的每一行都有黑色背景。似乎有些来自myapp.out的行输出时具有黑色背景颜色,但有些则没有。
我查看了这个Stack Overflow问题,但它并没有完全解决这个问题。我只想让Get-Content命令中的蓝色背景变成黑色。为什么会发生这种情况,我该如何解决这个问题?
有什么建议吗?
-- 编辑 --
我附上了输出的图像,这样你们就可以看到它了。

Strangely colored output in Powershell

我认为解析文件是最好的方法,因为它已经编码了ANSI颜色。我确实看到了这个stackoverflow例子,可以保持ANSI颜色, 但使用那里找到的代码仍然不能准确地显示它。以下是我如何将其整合:

$host.ui.RawUI.WindowTitle = "Log Title"
$host.ui.RawUI.BackgroundColor = "black"

Clear-Host

Get-Content -Path 'C:\myapp.out' -Wait | 
        ForEach { 
            $split = $_.Split([char] 27)
            foreach ($line in $split)
              { if ($line[0] -ne '[')
                  { Write-Host $line }
                else
                  { if     (($line[1] -eq '0') -and ($line[2] -eq 'm')) { Write-Host $line.Substring(3) -NoNewline }
                    elseif (($line[1] -eq '3') -and ($line[3] -eq 'm'))
                      { # normal color codes
                        if     ($line[2] -eq '0') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Black       }
                        elseif ($line[2] -eq '1') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkRed     }
                        elseif ($line[2] -eq '2') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkGreen   }
                        elseif ($line[2] -eq '3') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkYellow  }
                        elseif ($line[2] -eq '4') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkBlue    }
                        elseif ($line[2] -eq '5') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkMagenta }
                        elseif ($line[2] -eq '6') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkCyan    }
                        elseif ($line[2] -eq '7') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Gray        }
                      }
                    elseif (($line[1] -eq '3') -and ($line[3] -eq ';') -and ($line[5] -eq 'm'))
                      { # bright color codes
                        if     ($line[2] -eq '0') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor DarkGray    }
                        elseif ($line[2] -eq '1') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Red         }
                        elseif ($line[2] -eq '2') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Green        }
                        elseif ($line[2] -eq '3') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Yellow      }
                        elseif ($line[2] -eq '4') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Blue        }
                        elseif ($line[2] -eq '5') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Magenta     }
                        elseif ($line[2] -eq '6') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Cyan        }
                        elseif ($line[2] -eq '7') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor White       }
                    }
                }
            }
        }

这确实“改善”了彩色日志文件的显示,但请注意第一行似乎被删除了标签,并且当应该是蓝色时,着色返回到白色:

enter image description here

我还添加了这个 ANSI 彩色日志的一部分要点文件,以更好地理解源代码:

  1. 文本文件没有颜色,它们只是文本。
  2. Write-Host 只将内容输出到主机窗口,而不是文件。
- Bill_Stewart
1
感谢对文本文件的澄清。我理解write-host的作用,我不想将其输出到新文件中。我想在具有黑色背景的Powershell终端窗口中显示我的应用程序创建的.out文件。 - ariestav
1
“似乎有些行输出时带有黑色背景,但有些则没有”是什么意思? - Bill_Stewart
我可能会从这个问题中获取正则表达式,并将其从sed/perl语法转换为.Net语法。 - Bacon Bits
@BaconBits 有没有可能帮忙解析一下?我在这里束手无策,因为我并不经常使用Powershell,你的帮助将是非常棒的。 - ariestav
显示剩余4条评论
1个回答

0

你的代码对我来说完全可用:

Before Clearing the Host ... enter image description here

如果您想得到更好的答案,您需要提供一些样本数据来进行测试。同时,也许您可以提供您的实际代码。我猜您正在解析从Get-Content输出的内容,并设置前景色;因为PowerShell不理解颜色编码。

谢谢,是的,它可以处理没有 ANSI 颜色或制表符的文件文本。但是我更新了问题以展示我现在如何解析它,以及一个包含 ANSI 颜色编码的日志部分的 Gist。 - ariestav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接