在 PowerShell 输出到文件之前修复 ANSI 控制字符 [英] Fix ANSI control characters before PowerShell output to a file
本文介绍了在 PowerShell 输出到文件之前修复 ANSI 控制字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
PowerShell 是否可以输出没有 ANSI 控制字符(如颜色控制)的文件,例如[1;xxm
或 [xm]
,在输出到文件之前,
Is there anyway for PowerShell to output a file without ANSI control characters like color control, e.x. [1;xxm
or [xm]
, before outputting to a file,
[1;35mStarting selenium server... [0m[1;35mstarted - PID: [0m 22860
[0;36m[Signin Test] Test Suite[0m
[0;35m================================[0m
Running: [0;32mstep 1 - launch the browser[0m
[1;35m[40mINFO[0m [1;36mRequest: POST /wd/hub/session[0m
输出在 PowerShell 终端中以颜色正确显示,(我使用了 chcp
,不起作用)
The output displays correctly with color in PowerShell terminal, (I've used chcp
, not working)
推荐答案
你可以试试这样的:
... | ForEach-Object {
$_ -replace '\[\d+(;\d+)?m' | Add-Content 'C:\path\to\output.txt'
$_
}
或将其包装在函数中:
function Tee-ObjectNoColor {
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[string]$InputObject,
[Parameter(Position=1, Mandatory=$true)]
[string]$FilePath
)
Process {
$InputObject -replace '\[\d+(;\d+)?m' | Add-Content $FilePath
$InputObject
}
}
... | Tee-ObjectNoColor -FilePath 'C:\path\to\output.txt'
这篇关于在 PowerShell 输出到文件之前修复 ANSI 控制字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文