How to execute a single shell command writing to stderr from PowerShell task?
See original GitHub issueWe are trying to run a single shell command from a powershell script (inline or not, doesn’t matter). Let’s take npm pack as an example:
- pwsh: |
npm pack .
The npm
command like most *nix-like commands is outputting user info on stderr
and result on stdout
; here this is particularly relevant because npm pack
outputs the package name, which we need for the next task. Anyway in general we might want to pipe the output to some other command, while messages to stderr
need to be logged to console so that they are visible to the pipeline maintainers for diagnosing purpose.
In bash this just works. Plain and simple. Just as expected. Always.
In cmd this almost work, aside from the fact capturing the result of a command requires an incredibly complex syntax compared to what it should be (result = command
).
Now PowerShell. By default PowerShell has this nonsensical behavior (at least on Azure, because locally it’s different of course) where writing to stderr
generates an exception and fails the task. On top of that the Azure powershell task captures those errors and print some log messages with a ##[error]
prefix.
The task has a failOnStderr
option. It has been broken for years, but it seems to behave better nowadays. We recently tried failOnStderr: false
and indeed the task succeeds… after logging 315 error messages, which are randomly reformatted in the logs, and appear prominently on the front page for the given pipeline build, strongly suggesting to the user that something went awfully wrong (it didn’t).
We tried:
Invoke-Expression
${ }
$ErrorActionPreference
failOnStderr
$LASTEXITCODE
try{} catch{}
block- Redirect to stdout
2>&1
, which for us doesn’t work since we need the package name output ofnpm pack
- Redirect to file
2>err.txt
- Any combination of the above one can think of.
Several Microsoft engineers across 2 teams spent 10+ hours writing at least 3 different variants, all of which currently used in production for lack of a better alternative, all of which partially broken, all of which randomly failing with minor command or pipeline changes.
I do not believe that this is the amount of work that should be required to write such a simple task.
So here’s the simple question: how do we do this in PowerShell, with proper error handling?
PACKAGE=$(npm pack .)
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (13 by maintainers)
Hi @djee-ms, I’m closing it currently due to inactivity, please feel free to reopen it if it still actual for you
Hi @ralph-tice could you please describe your case in more details and provide some repro steps/sample? You can use errorActionPreference and failOnStderr task inputs - to control Powershell behavior regarding stderr.