0

I'm using the following PowerShell task:

2022-12-09T04:30:32.5910772Z Task         : PowerShell
2022-12-09T04:30:32.5911006Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2022-12-09T04:30:32.5911196Z Version      : 2.212.0
2022-12-09T04:30:32.5911349Z Author       : Microsoft Corporation
2022-12-09T04:30:32.5911615Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell

Here is an example warning output where you can see the message is surrounded by [33;1m and [0m. I assume these are ANSI escap codes.

2022-12-09T04:30:30.8803823Z [33;1mWARNING: The provided service principal secret will be included in the 'AzureRmContext.json' file found in the user profile ( /root/.Azure ). Please ensure that this directory has appropriate protections.[0m

Here is an example Error message output:

2022-12-09T04:30:39.5319115Z [91mInvoke-RestMethod: [0m/azp/agent/_work/r2/a/_ADsync-Utility/adsync/ADsyncUtility/adbusersync.ps1:254
2022-12-09T04:30:39.5320715Z [96mLine |
2022-12-09T04:30:39.5322096Z [96m 254 | [0m … sResponse = [96mInvoke-RestMethod $adbGroupsURL -Method GET -Headers $adb[0m …
2022-12-09T04:30:39.5323707Z [96m     | [91m               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-12-09T04:30:39.5325002Z [91m[96m     | [91m{"error_code":"403","message":"Invalid access to Org:
2022-12-09T04:30:39.5326245Z [96m     | [91m2660194586577136"}
2022-12-09T04:30:39.5327021Z [0m

As a workaround, is there a way that I can configure the format of Powershell Write-Verbose, Write-Warning and Write-Error cmdlets' messages to remove these escape codes and/or possibly use the Azure DevOps Logging Command Formatting syntax?

  • ##[warning]Warning message
  • ##[error]Error message
successhawk
  • 3,071
  • 3
  • 28
  • 44
  • Check if `$PSStyle.OutputRendering` is ANSI. If it is, change it to `Host` or `PlainText`: `$PSStyle.OutputRendering = 'PlainText'` (place it at the top of your code) – Santiago Squarzon Dec 09 '22 at 06:11
  • Have you checked the $PSStyle.OutputRendering? – Ging Yuan-MSFT Dec 16 '22 at 03:02
  • The value was 'Ansi'. Changing it to 'Host' did not fix it. Changing it to 'PlainText' fixed the formatting of warnings, but when I write-error I still get the ansi formatting. When I set the value does it only apply inside my script? Maybe the error is actually written outside of the context of my script because $ErrorActionPreference=Stop. I'm not sure. Any ideas? – successhawk Jan 20 '23 at 21:02

0 Answers0