When I explicitly click on my new object (see the first line of code), my newly created EntityReference will be stored without wrapping in PSObject, and therefore its serialization is just fine:
$entityRef1 = [Microsoft.Xrm.Sdk.EntityReference](new-object Microsoft.Xrm.Sdk.EntityReference("businessunit", [System.Guid]::NewGuid()))
$unit = new-object Microsoft.Xrm.Sdk.Entity("businessunit")
$unit.Attributes["parentbusinessunitid"] = $entityRef1
$unit.Attributes["parentbusinessunitid"].GetType()
$serializer = New-Object System.Runtime.Serialization.DataContractSerializer($unit.GetType())
$stream = New-Object System.IO.MemoryStream
$serializer.WriteObject($stream, $unit)
$stream.Flush()
$stream.Seek(0, [System.IO.SeekOrigin]::Begin)
$reader = New-Object System.IO.StreamReader($stream)
$reader.ReadToEnd()
However, when I do not use cast:
$entityRef1 = (new-object Microsoft.Xrm.Sdk.EntityReference("businessunit", [System.Guid]::NewGuid()))
Powershell complains when I want to serialize it:
Exception calling "WriteObject" with "2" argument(s): "Type 'System.Management.Automation.PSObject' with data contract name 'PSObject:http://schemas.datacontract.org/2004/07/System.Management.Automation' is not expected.
Now I read. Why does Get-Date seem to return DateTime objects, but does the BinarySerializer indicate that it returns a PSObject? , and it would seem that this is the same problem ....
except that I am using Powershell 3.0 ( $PSVersionTable.psversionproduces version 3.0.-1.-1) and that the "erroneous" code snippet from this message works fine in my Powershell environment ...
, , DLR Powershell 3, , - ?
: , SDK CRM. , powershell , System.UriBuilder, System.Management.Automation.PSObject:
$uriBuilder = (New-Object UriBuilder)
$dict = new-object Hashtable
$dict["mykey"] = $uriBuilder
$dict["mykey"].GetType()
$serializer = New-Object System.Runtime.Serialization.DataContractSerializer($dict.GetType())
$stream = New-Object System.IO.MemoryStream
$serializer.WriteObject($stream, $dict)
$stream.Flush()
$stream.Seek(0, [System.IO.SeekOrigin]::Begin)
$reader = New-Object System.IO.StreamReader($stream)
$reader.ReadToEnd()