Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alx9r opened this issue Mar 21, 2018 · 7 comments
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module

Comments

@alx9r
Copy link

alx9r commented Mar 21, 2018

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                                                                                        
@mklement0
Copy link
Contributor

mklement0 commented Mar 21, 2018

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.

@alx9r
Copy link
Author

alx9r commented Mar 21, 2018

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

@alx9r
Copy link
Author

alx9r commented Mar 21, 2018

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

Copy link
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.

1 similar comment
Copy link
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.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 16, 2023
Copy link
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.

Copy link
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
Labels
Issue-Bug Issue has been identified as a bug in the product Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module
Projects
None yet
Development

No branches or pull requests

4 participants