首页 > 解决方案 > PowerShell Select-String 导致 Esc 字符

问题描述

我正在尝试将一些日志组合在一起并仅选择具有高欠载 (u:) 的行。任何超过 0 的都是高的。该代码在终端内创造了奇迹。但是,当我将信息推送到文件时,我得到的是 ESC 字符而不是信息。

$Files = get-childitem C:\Tmp
$Underruns = foreach ($file in $Files) {
    $File | get-content | Select-String -Pattern ",u:[1-9].*,o:"
}
$Underruns | Out-File -Encoding utf8BOM -FilePath c:\tmp\combined.txt

上面的代码是添加转义字符。这些字符正在阻止代码的流动。我怎样才能防止这种情况发生?

(j:0[7m,u:15424,o:[0m0)

感谢您的帮助。

标签: powershellselect-string

解决方案


经过一些挖掘解决方案是选择字符串上的 -Raw 标志。

$Files = get-childitem C:\Tmp
$Underruns = foreach ($file in $Files) {
    $File | get-content | Select-String -Pattern ',u:[1-9].*,o:' -Raw
}
$Underruns | Out-File -Encoding utf8BOM -FilePath c:\tmp\combined2.txt

推荐阅读