当使用get-content回显到屏幕时,如何启用PowerShell解释ANSI颜色代码?

4

我有一个包含各种文本周围的ansi颜色代码的日志文件。我正在使用PowerShell语言命令将其回显到控制台:

get-content logfile.log -wait

我希望能够查看最新的日志更改。 然而,所有的ansi颜色代码都显示为文本字符,例如:

 Esc[90mEsc[39m

如何在PowerShell窗口中将它们解释为颜色代码?

对于不太熟悉PowerShell语言的人来说,是否有一种PowerShell命令或编码选项可以处理这个问题?我已经阅读了各种PowerShell文档,但没有找到任何关于这些ansi代码的信息。


2
你是否正在运行控制台 shell,powershell.exe?如果是的话,那么你可以尝试使用 ANSICON 或者 ConEmu。它们会注入一个 DLL 来钩取 Windows 控制台 API。 - Eryk Sun
1个回答

5
你可以通过在 ESC 处分割文本,并将颜色翻译成 Write-Host .... -Forground <color> 指令来翻译 ANSI 转义码的颜色。
function Open-Colored([String] $Filename)
  { Write-Colored(cat -Raw $Filename) }

function Write-Colored([String] $text)
  { # split text at ESC-char
    $split = $text.Split([char] 27)
    foreach ($line in $split)
      { if ($line[0] -ne '[')
          { Write-Host $line -NoNewline }
        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 Gree        }
                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       }
              }
          }
      }
  }

使用方法:

Open-Colored .\myColoredLogfile.log

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接