X Tutup
The Wayback Machine - https://web.archive.org/web/20201111214744/https://github.com/PowerShell/PowerShell/issues/7937
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

Select-Object -ExpandProperty hides ETS instance members #7937

Open
mklement0 opened this issue Oct 3, 2018 · 0 comments
Open

Select-Object -ExpandProperty hides ETS instance members #7937

mklement0 opened this issue Oct 3, 2018 · 0 comments

Comments

@mklement0
Copy link
Contributor

@mklement0 mklement0 commented Oct 3, 2018

Follow-up from #7768

If you use -ExpandProperty with a property whose value happens to be an object with instance members (ETS members added with -Add-Member), these members are no longer present in the value returned.

Note: The behavior only surfaces if the object is not an instance of [psobject] - that is, if it is neither a true custom object ("property bag") nor an incidental [psobject] wrapper (see #5579).

@PetSerAl discovered this problem in the context of #7768 and explains it as follows:

In v2 extra properties were linked to PSObject wrapper. So two PSObjects wrapping the same underlying object can have different set of extra properties. In v3 properties now linked to underlying object (with some exceptions), but not to PSObject wrapper. But in the process PowerShell devs decided to keep/grant Select-Object -ExpandProperty ability to create independent PSObject wrappers, which links properties to themselves rather than to underlying objects.
[...]
Although, I do not think Select-Object should really do this. I do not see reasons why it use/have this ability in the first place (compatibility? maybe).

A fix for this issue - to no longer attach the ETS members to the incidental wrapper and instead use the resurrection tables - should also fix the problem described in #12411.

Steps to reproduce

$v = [datetime]::now; $v | Add-Member myProp myPropValue
"[$($v.myProp)]"
'---'
# This should retrieve the original $v value, via Select-Object -ExpandProperty
 $vToo = [pscustomobject] @{ prop = $v } | Select-Object -ExpandProperty prop
"[$($vToo.myProp)]"

Expected behavior

[myPropValue]
---
[myPropValue]

That is, the .myProp instance member should still be present.

Actual behavior

[myPropValue]
---
[]

That is, the .myProp instance member is no longer present.

What presumably happens is that a [psobject] wrapper is created around the output object with storeTypeNameAndInstanceMembersLocally set to $true, which makes PowerShell look for instance members only on the wrapper object itself, effectively eclipsing any preexisting instance members associated with the wrapped object via the resurrection tables.

Environment data

PowerShell Core v6.1.0
@mklement0 mklement0 changed the title Select-Object -ExpandProperty quietly discards instance members Select-Object -ExpandProperty quietly discards ETS instance members Nov 19, 2019
@mklement0 mklement0 changed the title Select-Object -ExpandProperty quietly discards ETS instance members Select-Object -ExpandProperty hides ETS instance members Apr 21, 2020
@iSazonov iSazonov added the Issue-Bug label Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.
X Tutup