Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New-CrmEntityReference throws error in latest module versions #489

Open
ghost opened this issue Oct 26, 2021 · 5 comments
Open

New-CrmEntityReference throws error in latest module versions #489

ghost opened this issue Oct 26, 2021 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 26, 2021

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()
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-CrmRecordsByFetch
+ PSComputerName : localhost

OperationStopped: (:String) [], RuntimeException
+ CategoryInfo : OperationStopped: (:String) [], RuntimeException
+ FullyQualifiedErrorId :
+ PSComputerName : localhost

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
Copy link
Owner

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
Copy link
Author

ghost commented Oct 28, 2021

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
Copy link
Owner

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
Copy link
Author

ghost commented Oct 29, 2021

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
Copy link
Owner

seanmcne commented Nov 3, 2021

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant