Skip to content

Write-Output $null -NoEnumerate produces different Collection<PSObject> in 5.1 vs 6.0 #6451

Closed

Description

@alx9r

I'm not sure which behavior is better, but I expected 5.1 and 6.0 to behave the same in this respect. If this change is deliberate I would like to understand why.

Steps to reproduce

$r = [System.Collections.ObjectModel.Collection[psobject]]{Write-Output $null -NoEnumerate}.Invoke()
$r.item(0).GetType()

Behavior in PowerShell 5.1

You cannot call a method on a null-valued expression.
At C:\Users\un1\Desktop\test.ps1:2 char:1
+ $r[0].GetType()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
>$PSVersionTable

Name                           Value                                                                    
----                           -----                                                                    
PSVersion                      5.1.14409.1012                                                           
PSEdition                      Desktop                                                                  
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                  
BuildVersion                   10.0.14409.1012                                                          
CLRVersion                     4.0.30319.42000                                                          
WSManStackVersion              3.0                                                                      
PSRemotingProtocolVersion      2.3                                                                      
SerializationVersion           1.1.0.1                                                                  

Behavior in PowerShell 6.0

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSObject[]                               System.Array
> $PSVersionTable

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

Activity

  1. mklement0 commented on Mar 21, 2018

    @mklement0
    Contributor

    Indeed, Write-Output is broken - see #5955 and #5122, and it all seems to be the result of the ill-fated #2038 PR that arose out of issue #2035.

  2. alx9r commented on Mar 21, 2018

    @alx9r
    Author

    Oi. That's a messy broken business, indeed.

  3. alx9r commented on Mar 21, 2018

    @alx9r
    Author

    It looks like this change to Write-Output is the culprit. Copying the command from the revision prior and renaming to Write-Output2 seems to result in behavior consistent with PowerShell 5.1:

    Add-Type '
        using System.Management.Automation;
    
        [Cmdlet(VerbsCommunications.Write, "Output2")]
        public class WriteOutput2Command : PSCmdlet
        {
            private PSObject[] _inputObjects = null;
    
            [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromRemainingArguments = true)]
            [AllowNull]
            [AllowEmptyCollection]
            public PSObject[] InputObject
            {
                get { return _inputObjects; }
                set { _inputObjects = value; }
            }
    
            [Parameter()]
            public SwitchParameter NoEnumerate
            {
                get;
                set;
            }
    
            protected override void ProcessRecord()
            {
                if (null == _inputObjects)
                {
                    WriteObject(_inputObjects);
                    return;
                }
    
                bool enumerate = true;
                if (NoEnumerate.IsPresent)
                {
                    enumerate = false;
                }
                foreach (PSObject inputObject in _inputObjects)
                {
                    WriteObject(inputObject, enumerate);
                }
            }//processrecord
        }' -PassThru | % Assembly | Import-Module
    
    $r = [System.Collections.ObjectModel.Collection[psobject]]{Write-Output2 $Null -NoEnumerate}.Invoke()
    $r.Item(0).GetType()

    outputs

    You cannot call a method on a null-valued expression.
    At C:\users\un1\Desktop\test2.ps1:46 char:1
    + $r.Item(0).GetType()
    + ~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    
  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions