Colour-coding get-content results

二次信任 提交于 2019-11-30 07:21:27

Try

Get-Content $logFile -Wait |
  Select-String $searchTerm | 
  ForEach {write-host -ForegroundColor red $_.line}

If you're looking for something that provides selective color coding, then try something like this.

First, set up a helper function to select an appropriate color:

function Get-LogColor {
    Param([Parameter(Position=0)]
    [String]$LogEntry)

    process {
        if ($LogEntry.Contains("DEBUG")) {Return "Green"}
        elseif ($LogEntry.Contains("WARN")) {Return "Yellow"}
        elseif ($LogEntry.Contains("ERROR")) {Return "Red"}
        else {Return "White"}
    }
}

Then execute a line that looks like this:

gc -wait $logFile | ForEach {Write-Host -ForegroundColor (Get-LogColor $_) $_}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!