Colour-coding get-content results

后端 未结 2 1995
时光说笑
时光说笑 2020-12-15 11:34

I\'ve got a powershell script that monitors a log file, filters out the interesting bits and then presents those bits to me as and when they are written to the file. Works w

相关标签:
2条回答
  • 2020-12-15 12:11

    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 $_) $_}
    
    0 讨论(0)
  • 2020-12-15 12:15

    Try

    Get-Content $logFile -Wait |
      Select-String $searchTerm | 
      ForEach {write-host -ForegroundColor red $_.line}
    
    0 讨论(0)
提交回复
热议问题