我有一个日志文件,其中包含了颜色编码,并且我想要使用 Powershell 来“跟踪尾部”查看日志。当我调用下面的脚本时,Powershell 会以其标准的蓝色背景启动,并且在日志文件中编码的颜色也会显示出来。
现在,这确实有点改变了,但并不是使得
我查看了这个Stack Overflow问题,但它并没有完全解决这个问题。我只想让
有什么建议吗?
-- 编辑 --
我附上了输出的图像,这样你们就可以看到它了。
这确实“改善”了彩色日志文件的显示,但请注意第一行似乎被删除了标签,并且当应该是蓝色时,着色返回到白色: 我还添加了这个 ANSI 彩色日志的一部分要点文件,以更好地理解源代码:
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
命令中的蓝色背景变成黑色。为什么会发生这种情况,我该如何解决这个问题?有什么建议吗?
-- 编辑 --
我附上了输出的图像,这样你们就可以看到它了。
我认为解析文件是最好的方法,因为它已经编码了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 }
}
}
}
}
这确实“改善”了彩色日志文件的显示,但请注意第一行似乎被删除了标签,并且当应该是蓝色时,着色返回到白色: 我还添加了这个 ANSI 彩色日志的一部分要点文件,以更好地理解源代码:
Write-Host
只将内容输出到主机窗口,而不是文件。