Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Adding an array item with += drops custom AliasProperty

$
0
0

Code:

$a = @(1,2,3) Get-Member -InputObject $a -MemberType AliasProperty "`nadd AliasProperty 'size'" $a = Add-Member -InputObject $a -MemberType AliasProperty -Name 'size' -Value 'Length' -PassThru Get-Member -InputObject $a -MemberType AliasProperty "`na.size=$($a.size) ($($a -join ','))""a.length=$($a.length)""a.count=$($a.count)" $a += 4"`nAdded item to array and AliasProperty 'size' is gone!" Get-Member -InputObject $a -MemberType AliasProperty "`na.size=$($a.size) ($($a -join ','))" + ' *** notice the missing value for $a.size'"a.length=$($a.length)""a.count=$($a.count)" "`nadd AliasProperty to array again" $a = Add-Member -InputObject $a -MemberType AliasProperty -Name 'size' -Value 'Length' -PassThru"" Get-Member -InputObject $a -MemberType AliasProperty "`na.size=$($a.size) ($($a -join ','))""a.length=$($a.length)""a.count=$($a.count)"

Output:

   TypeName: System.Object[]
Name  MemberType    Definition    
----  ----------    ----------    
Count AliasProperty Count = Length
add AliasProperty 'size'
Count AliasProperty Count = Length
size  AliasProperty size = Length 
a.size=3 (1,2,3)
a.length=3
a.count=3
Added item to array and AliasProperty 'size' is gone!
Count AliasProperty Count = Length
a.size= (1,2,3,4) *** notice the missing value for $a.size
a.length=4
a.count=4
add AliasProperty to array again
Count AliasProperty Count = Length
size  AliasProperty size = Length 
a.size=4 (1,2,3,4)
a.length=4
a.count=4

Is there another way to add an array item and preserve the custom AliasProperty?

Thanks,

Jim Cox

 


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>