-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Follow-up from #7768
If you use -ExpandProperty with a property whose value happens to be an object with instance ETS 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