【发布时间】:2018-01-07 05:48:12
【问题描述】:
我有一个包含颜色编码的日志文件,我想使用 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 中的某些行以黑色背景颜色输出,但有些则不是。
我查看了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只写入主机,不写入文件。 -
感谢您澄清什么是文本文件。我了解 write-host 的作用,并且我对输出到新文件不感兴趣。我有兴趣在黑色背景的 Powershell 终端窗口中显示我的应用程序创建的 .out 文件。
-
“似乎有些行输出为黑色背景,但有些不是”是什么意思?
-
我可能会从 this question 获取正则表达式并将其从 sed/perl 语法转换为 .Net 语法。
-
@BaconBits .Net 语法在 Powershell 中有效吗?我想我不确定在这个脚本中引入正则表达式的最佳方法。此外,我的理解是,并非所有正则表达式解释器的工作方式都相同。
标签: shell powershell scripting