windows - How do you enable powershell to interpret ansi color codes when using get-content to echo to the screen? -



windows - How do you enable powershell to interpret ansi color codes when using get-content to echo to the screen? -

i have log file contains ansi color codes around various text. i'm echoing console using powershell language command:

get-content logfile.log -wait

so can see latest log changes. however, ansi color codes show text characters like:

esc[90mesc[39m

how them intepreted color codes in powershell window?

not beingness familiar powershell language yet, there powershell command or encoding alternative handle this? i've read various powershell docs haven't found in them re these ansi codes.

you can translate ansi escape codes colors splitting text @ esc , translating colors write-host .... -forground <color> instructions.

function open-colored([string] $filename) { write-colored(cat -raw $filename) } function write-colored([string] $text) { # split text @ 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 grayness } } 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 reddish } 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 yellowish } elseif ($line[2] -eq '4') { write-host $line.substring(6) -nonewline -foregroundcolor bluish } 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 } } } } }

usage:

open-colored .\mycoloredlogfile.log

windows powershell batch-file ansi-escape

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -