seanmcne / Microsoft.Xrm.Data.PowerShell

This module uses the CRM connection from Microsoft.Xrm.Tooling.CrmConnector.Powershell and provides common functions to create, delete, query, and update data as well as functions for common tasks such as publishing, and manipulating System & CRM User Settings, etc. The module should function for both Dynamics CRM Online and On-Premise environment.
204 stars 65 forks source link

New-CrmEntityReference throws error in latest module versions #489

Open ghost opened 2 years ago

ghost commented 2 years ago

On Windows Server 2012 R2 with PowerShell 4.0 we had to downgrade the module from v2.8.14 to v2.8.11 because we started receiving the following error when fetching CRM data:

System.Management.Automation.RuntimeException: Method invocation failed because [Microsoft.Xrm.Sdk.EntityReference] does not contain a method named 'new'. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0) at System.Management.Automation.PSScriptCmdlet.RunClause(Action1 clause, Object dollarUnderbar, Object inputToProcess) at System.Management.Automation.PSScriptCmdlet.DoEndProcessing() at System.Management.Automation.CommandProcessorBase.Complete()

OperationStopped: (:String) [], RuntimeException

We traced the issue to the implementation of the New-CrmEntityReference cmdlet, where the previous method of creating a new EntityReference

$crmEntityReference = New-Object -TypeName Microsoft.Xrm.Sdk.EntityReference

has been replaced by

$crmEntityReference = [Microsoft.Xrm.Sdk.EntityReference]::new()

We assume this has something to do with the PS version, since on Windows 10 the latest module version works perfectly.

seanmcne commented 2 years ago

Thanks for reaching out - after doing performance testing we made these changes as it helped improve performance by a good margin (especially for folks working with moderately large sized record sets).

Can you try this to see if it works in PowerShell 4.0?

[system.datetime]::now
ghost commented 2 years ago

Thanks for the response.

[system.datetime]::now correctly returns the current date and time on the affected system.

As a side note, I tried tab completion on the term [Microsoft.Xrm.Sdk.EntityReference]:: and got offered only two functions:

[Microsoft.Xrm.Sdk.EntityReference]::Equals()
[Microsoft.Xrm.Sdk.EntityReference]::ReferenceEquals()

I also browsed through the PowerShell documentation available on docs.microsoft.com, and it seems that formal support for classes and the [<class-name>]::new( syntax only appeared in PowerShell 5.0

seanmcne commented 2 years ago

That will definitely be an issue, unfortunately. I can go back to try the comparison again to see how much of a performance increase there is to see if it's worth going back to new-object or not?

ghost commented 2 years ago

No I don't think that's necessary, thank you. We can get by using the older module version - the main thing is that we got our integrations working again. Just wanted you to know about our issue. Also, we'll probably look shortly into either installing Management Framework 5 on the server, or upgrading the entire server since 2012R2 is nearing it's EOL (Oct 2023).

However, if I may suggest, if you still intend to support PowerShell v4 as a platform for the module, would it be possible to implement alternative execution paths where required? I mean, for example, use the old statement New-Object -TypeName Microsoft.Xrm.Sdk.EntityReference instead of [Microsoft.Xrm.Sdk.EntityReference]::new() if the PS version is 4. There may of course be some other, better ways to work around this one specific issue.

seanmcne commented 2 years ago

I really appreciate you sharing this detail, I'm wondering if it might make sense to update our manifest to support v5 since we now have v5 specific features we're now using, also I know many platforms that run WMF v4 are going end of life (not quite yet, but soon).