Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New-Object -Property Hashtable not populating fields in Version 5.0 on Win 10

The follow code works fine in PowerShell 4.0 and earlier. If I run it on version 5.0.10240.16384 on the latest Windows 10 build the fields don't populate in the new object ($a). Is this a bug or has something changed?

Add-Type @"
   public struct TestUser {
    public string First;
    public string Last;
}
"@;

$a = New-Object TestUser -Property @{ First = "Joe"
                                Last = "Smith"};

Version 4.0 and earlier results:

$a.First -eq "Joe"
$a.Last -eq "Smith"

Version 5.0

$a.First -eq $null
$a.Last -eq $null

Version 5 Get-Member

TypeName: TestUser

Name        MemberType Definition                    
 ----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
ToString    Method     string ToString()             
First       Property   string First {get;set;}       
Last        Property   string Last {get;set;}      

EDIT: I've submitted a bug regarding this to Microsoft based on this being a breaking change (if it is a change) that will impact many existing scripts. I will update this question if there are any updates on that bug report. I'm still looking for any feedback if anyone has run across something in v5 that might suggest this is the desired/changed behavior. Glad to try other testing on v5 as well if anyone is curious and doesn't have access to a Win 10 machine.

https://connect.microsoft.com/PowerShell/feedbackdetail/view/1552941/new-object-property-hashtable-not-populating-fields-in-version-5-0-on-win-10

like image 405
Matt Gartman Avatar asked Jul 16 '15 15:07

Matt Gartman


1 Answers

I've gotten verification from a PowerShell team member that this is a bug related to a missing "unbox" call. The workaround for now is to use a class instead of a struct if possible. It was mentioned that struct provides little benefit in PowerShell, as PowerShell will always box a struct.

like image 121
Keith Hill Avatar answered Nov 06 '22 04:11

Keith Hill