question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to execute a single shell command writing to stderr from PowerShell task?

See original GitHub issue

We 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 of npm 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:closed
  • Created 3 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
anatolybolshakovcommented, Sep 11, 2020

Hi @djee-ms, I’m closing it currently due to inactivity, please feel free to reopen it if it still actual for you

0reactions
anatolybolshakovcommented, Apr 22, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running commands in the shell - PowerShell | Microsoft Learn
This article shows how to run commands in PowerShell.
Read more >
How do I write to standard error in PowerShell? - Stack Overflow
Use Write-Error to write to stderr. To redirect stderr to file use: Write-Error "oops" 2> /temp/err.msg.
Read more >
What is STDOUT and STDERR in Linux - echo to STDERR
A Best Practice to echo the stdout and stderr into two different files in Shell script – LOGGING. Let's say you are creating...
Read more >
Executing shell commands, script files, and executables in Go
While working with any programming language, you have to admit that not everything can be written in a program. Sometimes, you need to...
Read more >
Shell Scripts - Provisioning | Vagrant - HashiCorp Developer
args (string or array) - Arguments to pass to the shell script when executing it as a single string. These arguments must be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found