sil*_*ent 8 powershell azure-devops
我正在 Powershell(或 Powershell Core,两者都是此处的选项)中运行命令 ( New-AzResourceGroupDeployment -WhatIf),它会产生一些非常丰富多彩的输出,如下所示:
问题是,当我在 Azure DevOps 管道中运行它时,那里的日志记录会被颜色混淆并产生大量乱码:

所以我的问题是:由于这个命令本身没有这样的选项,PowerShell(核心)中是否有通用方法来禁用命令产生彩色输出?
Hil*_*ion 15
PowerShell 7.2 可能为您提供新的解决方案:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText;
Run Code Online (Sandbox Code Playgroud)
来自about_ANSI_Terminals文档:
以下成员控制如何或何时使用 ANSI 格式:
$PSStyle.OutputRendering是一个System.Management.Automation.OutputRendering具有以下值的枚举:
- ANSI:ANSI 始终按原样传递
- PlainText:ANSI 转义序列始终被删除,因此它只是纯文本
- 主持人:这是默认行为。ANSI 转义序列在重定向或管道输出中被删除。
您还可以尝试查询PowerShell帮助系统:
Get-Help -Name 'about_ANSI_Terminals';
Run Code Online (Sandbox Code Playgroud)
与您拥有的东西(无论模块等)一起使用的完整示例。这:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::Ansi;
$PSVersionTable | Format-List;
Run Code Online (Sandbox Code Playgroud)
将产生彩色输出,如下所示:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText;
$PSVersionTable | Format-List;
Run Code Online (Sandbox Code Playgroud)
将产生纯文本。如果你尝试这样做:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::Host;
$PSVersionTable | Format-Table | Tee-Object -FilePath '.\output.txt';
Run Code Online (Sandbox Code Playgroud)
您应该在终端/控制台中得到彩色输出,但在文件中得到纯文本output.txt。
我还发现,这个问题已经在这里得到了回答并进行了更详细的描述。
| 归档时间: |
|
| 查看次数: |
4198 次 |
| 最近记录: |