-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
I discovered recently that the handling of stderr from native commands isn't being dealt with consistently between Windows PowerShell and Windows PowerShell ISE.
Steps to reproduce
$errorActionPreference="stop"; try { cmd /c nosuchexe } catch { 'dang!' }Actual ISE behavior (expected)
dang!
Native commands writing to stderr while $errorActionPreference="stop" generate a catchable NativeCommandError which encapsulates the original error message in a ErrorRecord. It is easy to capture, analyze, and reword error messages in context.
Actual CLI behavior
'nosuchexe' is not recognized as an internal or external command,
operable program or batch file.
No NativeCommandError is generated, no matter how $errorActionPreference is set. The stderr from the native command is piped straight through to the default process stderr.
This behavior is a serious loss of functionality, leading some to believe that PowerShell can't distinguish between stderr and stdout. Lots of community confusion generated because of the discrepency. See https://stackoverflow.com/questions/44488202/powershell-streaming-output/44491420#44491420, among others.
Analysis
The difference isn't just the ISE, I think it is related to PowerShell remote runspaces! The full info on the caught error record (in the ISE) demonstrates the underlying exception is a RemoteException like this:
PS C:\Users\Burt_> $errorActionPreference="stop"; try { cmd /c nosuchexe } catch { Format-List * -force -InputObject $_ }
PSMessageDetails :
Exception : System.Management.Automation.RemoteException: 'nosuchexe' is not recognized as an internal or external
command,
TargetObject : 'nosuchexe' is not recognized as an internal or external command,
CategoryInfo : NotSpecified: ('nosuchexe' is ...ternal command,:String) [], RemoteException
FullyQualifiedErrorId : NativeCommandError
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}Environment data
Both powershell.exe and powershell_ise.exe report the same version information:
Name Value
---- -----
PSVersion 5.1.15063.296
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.296
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1