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

时间:2017-07-31 15:13:39

标签: shell powershell scripting

我有一个日志文件,其中包含了颜色编码,我希望"跟随尾巴" 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命令的蓝色背景为黑色。为什么会发生这种情况,我该如何解决这个问题?

有什么建议吗?

- 编辑 -

我附加了输出图像。所以你们都能看到它。

Strangely colored output in Powershell

我认为解析文件是最好的,因为它已经编码了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       }
                    }
                }
            }
        }

这确实"改善"彩色日志文件的显示,但请注意第一行上的标签似乎是如何删除的,当它应该是蓝色时,着色会返回白色:

enter image description here

我还添加了a gist of a portion of this ANSI colored log文件以更好地了解来源:

1 个答案:

答案 0 :(得分:0)

您的代码适用于我:

Before Clearing the Host ... enter image description here

如果您想要更好的答案,则需要提供一些样本数据。也许,也许你的实际代码。我假设您正在解析Get-Content的输出并设置前景色;由于powershell不了解颜色编码。