我有一个日志文件,其中包含了颜色编码,并且我想用PowerShell跟踪日志文件的尾部。当我调用以下脚本时,Powershell将以其标准蓝色背景启动,并且日志文件中编码的颜色显示出来。使用Get-Content时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
的一些行以黑色背景色输出,但有些不是。
我看了this Stack Overflow question,但是这并没有完全解决问题。我只想让Get-Content
命令的蓝色背景变黑。为什么会发生这种情况,我该如何解决这个问题?
有什么建议吗?
- 编辑 -
我附上输出图像。所以你们都可以看到它。
我想解析该文件是最好的,因为它已经ANSI编码的颜色。我确实看到this stack overflow example of maintaining the ANSI colors,但使用找到的代码仍然无法准确显示。这里是我已经把它组成:
$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 }
}
}
}
}
这并“提高”有色日志文件的显示,但是请注意标签如何似乎在第一行被删除,并且着色返回到白色时它应该是一个蓝色:
我还添加了a gist of a portion of this ANSI colored log文件,以了解源更好:
1)文本文件没有颜色。他们只是文字。 2)'Write-Host'只写入主机,而不写入文件。 –
感谢您澄清哪些文本文件。我了解写主机做什么,并且我不想输出到新文件。我有兴趣显示我的应用程序在黑色背景的Powershell终端窗口中创建的.out文件。 – ariestav
“看起来好像有些线条是用黑色背景色输出的,但有些不是”是什么意思? –