Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 使用“获取内容”回显到屏幕时,如何使powershell解释ansi颜色代码?_Windows_Powershell_Batch File_Ansi Escape - Fatal编程技术网

Windows 使用“获取内容”回显到屏幕时,如何使powershell解释ansi颜色代码?

Windows 使用“获取内容”回显到屏幕时,如何使powershell解释ansi颜色代码?,windows,powershell,batch-file,ansi-escape,Windows,Powershell,Batch File,Ansi Escape,我有一个日志文件,其中包含各种文本周围的ansi颜色代码。我正在使用powershell语言命令将其回显到控制台: get-content logfile.log -wait 所以我可以看到最新的日志更改。但是,所有ansi颜色代码都显示为文本字符,如: Esc[90mEsc[39m 如何在powershell窗口中将它们作为颜色代码进行解释 由于还不太熟悉powershell语言,是否有powershell命令或编码选项来处理此问题?我已经阅读了各种powershell文档,但没有在其中

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

get-content logfile.log -wait
所以我可以看到最新的日志更改。但是,所有ansi颜色代码都显示为文本字符,如:

 Esc[90mEsc[39m
如何在powershell窗口中将它们作为颜色代码进行解释


由于还不太熟悉powershell语言,是否有powershell命令或编码选项来处理此问题?我已经阅读了各种powershell文档,但没有在其中找到任何有关这些ansi代码的内容。

您可以通过在ESC处拆分文本并将颜色转换为
写入主机来翻译颜色的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

您正在运行控制台shell powershell.exe吗?如果是这样的话,那么你可以试试看。它们注入了一个挂钩Windows控制台API的DLL。