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.

System.Management.Automation.PowerShell returns warning messages as error messages

See original GitHub issue

Steps to reproduce


Expected behavior

 PowerShell redirects warning messages to warning streams

Actual behavior

Directly invoke cli: image invoke through powershell: image

It turns out PowerShell redirects warning messages to error streams

Environment data

Name Value


PSVersion 7.0.0 PSEdition Core GitCommitId 7.0.0 OS Microsoft Windows 10.0.19042 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jborean93commented, Mar 29, 2021

That part doesn’t make much sense to me, maybe having errorOutput be null causes issues trying to do += in your ErrorDataRecieved event handler.

Regardless here is a reproducer for all this behaviour by using something I know for sure writes to stdout/stderr:

$proc = [Diagnostics.Process]::new()
$proc.StartInfo.FileName = 'powershell.exe'
$proc.StartInfo.Arguments = "-Command [Console]::Error.WriteLine('stderr'); [Console]::Out.WriteLine('stdout')"
$proc.StartInfo.UseShellExecute = $false
$proc.StartInfo.RedirectStandardError = $true
$proc.StartInfo.RedirectStandardOutput = $true

$proc.Start()
$proc.WaitForExit()

$stdout = $proc.StandardOutput.ReadToEnd()
$stderr = $proc.StandardError.ReadToEnd()

"STDOUT: {0}`nSTDERR: {1}`nRC: {2}" -f $stdout, $stderr, $proc.Exitcode

You can see that running this same command as a PowerShell script writes the stderr to the error stream

$ps = [PowerShell]::Create()
$stdout = $ps.AddScript('powershell.exe -Command "[Console]::Error.WriteLine(''stderr''); [Console]::Out.WriteLine(''stdout'')"').Invoke()[0]

$stderr = $ps.Streams.Error[0]

"OUTPUT: {0}`nERROR: {1}`nHAD_ERRORS: {2}" -f $stdout, $stderr, $ps.HadErrors

I unfortunately don’t have bicep installed or an environment set up to test it out but using these examples show how stderr data is sent to the error stream.

1reaction
BethanyZhoucommented, Mar 30, 2021

Thank you all @jborean93 , @mklement0 for explanation!!! Will communicate with bicep team and close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Powershell warning and error handling
1 Answer 1 ... WarningAction does not work like that. From the about_CommonParameters documentation: Determines how the cmdlet responds to a ...
Read more >
Get-Error (Microsoft.PowerShell.Utility)
The Get-Error cmdlet gets a PSExtendedError object that represents the current error details from the last error that occurred in the session.
Read more >
Everything you wanted to know about exceptions
An Exception is like an event that is created when normal error handling can't deal with the issue. Trying to divide a number...
Read more >
about Preference Variables - PowerShell
Ignore : Suppresses the error message and continues to execute the command. The Ignore value is intended for per-command use, not for use...
Read more >
Adding User Messages to Your Cmdlet - PowerShell
Warning messages that contain a notification that the cmdlet is about to perform an operation that can have unexpected results. Progress report ...
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