Home Dashboard Directory Help
Search

PSCustomObject and Select-Object Expandproperty by bholliger


Status: 

Active


1
0
Sign in
to vote
Type: Bug
ID: 770131
Opened: 11/5/2012 2:04:46 PM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Hi there!

I created the following pscustomobject with a nested pscustomobject. Interestingly after Select-Object -Expandproperty the source object is changed. Do you have an answer on that?

# Create array for subobject object
$subobject = @()
$subobject += [pscustomobject]@{'FieldName'='Value1';'FieldNameX'='Value2'}
$subobject += [pscustomobject]@{'FieldName'='Value3';'FieldNameX'='Value4'}

# Create hashtable
$arr = @{}
$arr.Object1 = $subobject
$arr.NameX = 'ValueX'

Write-Host 'source array:'
$arr

# create pscustomobject
$myobject = [pscustomobject]$arr

Write-Host 'myobject before output with select-object -expandproperty:'
$myobject

Write-Host 'myobject output with select-object -expandproperty:'
$myobject | Select-Object * -ExpandProperty Object1

Write-Host 'myobject after output with select-object -expandproperty:'
$myobject

--- After running the script $myobject is changed:

NameX : ValueX
Object1 : {@{FieldName=Value1; FieldNameX=Value2; NameX=ValueX; Object1=System.Object[]}, @{FieldName=Value3; FieldNameX=Value4; NameX=ValueX; Object1=System.Object[]}}

--- Expected content of myobject:

NameX : ValueX
Object1 : {@{FieldName=Value1; FieldNameX=Value2}, @{FieldName=Value3; FieldNameX=Value4}}

Thank you!

Have a great day!

-Bruno.
Details
Sign in to post a comment.
Sign in to post a workaround.