Skip to content

Select-Object -First populates -ErrorVariable #9185

Closed

Description

@Jaykul

Steps to reproduce

& {
    [CmdletBinding()]param()
    1,2,3,4 | Select -First 1
} -ErrorVariable Problems; 
$Problems |  Format-List * -Force

Expected behavior

There are no errors, so $Problems should be empty.

Actual behavior

$Problems has StopUpstreamCommandsException which Select-Object -First threw.

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.18353
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Mar 21, 2019
  2. vexx32 commented on Mar 21, 2019

    @vexx32
    Collaborator

    sigh

    We need a proper pipeline stop signal. This has been brought up before, and it really needs to be done. There are too many side effects from using a secret exception to do it.

  3. mossrich commented on Mar 29, 2019

    @mossrich

    Also reported 11/2018: https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/36081100-error-select-object-with-first-parameter-populat

    This issue is also present in Powershell 4 and 5, but manifests as 'System error. '

    PS > 1..2 | Select -First 2 -ErrorVariable X; $X | select *
    1
    2

    RequestingCommandProcessor : Select-Object
    Message : System error.
    Data : {}
    InnerException :
    TargetSite : Void ProcessRecord()
    StackTrace : at Microsoft.PowerShell.Commands.SelectObjectCommand.ProcessRecord()
    at System.Management.Automation.CommandProcessor.ProcessRecord()
    HelpLink :
    Source : Microsoft.PowerShell.Commands.Utility
    HResult : -2146233087

    PS > $PSVersionTable.PSVersion

    Major Minor Build Revision


    4 0 -1 -1

  4. ericnelson commented on Aug 28, 2019

    @ericnelson

    This one bites you in really fun ways when using orchestration tools like RunDeck that strictly check the ErrorVariable of Invoke-Expression -ErrorVariable +myerr. Scripts run but the orchestration tools report failures. Quite a few refactors because of this.

  5. vexx32 commented on Aug 28, 2019

    @vexx32
    Collaborator

    Steve Lee (@SteveL-MSFT) is there anything we can do about this one for ps7? I'm more than willing to put some work in here, but I'm not at all clear on where to start :(

  6. SteveL-MSFT commented on Aug 28, 2019

    @SteveL-MSFT
    Member

    Rain Sallow (/u/ta11ow) (@vexx32) It seems that this is not going to be a small fix that maintains backwards compatibility. As such, seems risky to take for PS7.

  7. vexx32 commented on Aug 28, 2019

    @vexx32
    Collaborator

    Sure, but that will always be the case. Lots of little niggles like this all add up to a bit of an unfortunate mess in terms of error handling in PS. Joel B has several issues on various aspects of it, and all of them could potentially be breaking changes.

    However, the behaviour is broken to begin with. If a major version isn't a time to fix these long-standing problems, when is? 🙂

  8. SteveL-MSFT commented on Aug 29, 2019

    @SteveL-MSFT
    Member

    I agree this should be addressed, however, it's also been like this for probably ever. From a priority standpoint, it's not clear to me how many customers are actually affected by this.

  9. vexx32 commented on Aug 30, 2019

    @vexx32
    Collaborator

    Definitely fair, I get that. I'm not asking for it to be solved yesterday, just if there are folks who might be familiar with the internals on your team, I'd appreciate some pointers on possible avenues for a solution / relevant code areas to be have a good chance of being able to put a solution in code. 🙂

  10. mossrich commented on Sep 4, 2019

    @mossrich

    In case it helps with prioritization, this showed up as a post-implementation issue for multiple clients at my organization when an internal product upgrade was applied that introduced 'Select -First' in a deploy script. Wrapper scripts falsely reported the error variable being set as a deploy failure. I added a workaround that ignores errors with 'HResult = -2146233087' in the most urgent cases, but I know there are some clients who haven't yet encountered this issue and will eventually.
    It seems safe to assume that no one is depending on the error variable to be set in this scenario.
    Thanks for whatever attention you can give to the solution. It may not help my clients who are unlikely to do a major version upgrade in the near future, but it certainly adds to the overall robustness of Powershell.

  11. SteveL-MSFT commented on Oct 18, 2019

    @SteveL-MSFT
    Member

    Looked into this and turned out to be simple

  12. changed the title [-]Select-Object -First populates the ERROR Variable[/-] [+]Select-Object -First populates `-ErrorVariable`[/+] on Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-QuestionResolution-Fixed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions