Skip to content

Connection fails if some parameters contain single quotes passed to the CrmServiceClient constructor. #518

@break9111

Description

@break9111

I have identified the following error, which I think needs to be resolved.

How to reproduce the error
When connecting to Dataverse using Connect-CrmOnline, an error occurs if the user's password contains single quotes.

Description of the error

PS C:\Users\aaaa> Connect-CrmOnline  -ServerUrl https://org27XXXXXX.crm.dynamics.com -Credential $cred
New-Object : Exception calling ".ctor" with "1" argument(s): "Object reference not set to an instance of an object."
At C:\Users\aaaa\Documents\WindowsPowerShell\Modules\Microsoft.Xrm.Data.Powershell\2.8.19\Microsoft.Xrm.Data.Powershel
l.psm1:255 char:19
+ ... obal:conn = New-Object Microsoft.Xrm.Tooling.Connector.CrmServiceClie ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Cause of error
When calling the CrmServiceClient constructor, the input parameters for Connect-CrmOnline are concatenated into a single string and passed to the constructor as follows.
If the password contains single quotes, the process of parsing this concatenated string will fail, resulting in an error before any authentication process is performed.

$global:conn = New-Object Microsoft.Xrm.Tooling.Connector.CrmServiceClient -ArgumentList $cs
##value##
$cs = "RequireNewInstance=True;Url={your Dataverse environment URL};AuthType=Office365;Username=testuser@aaaaa.onmicrosoft.com;Password='Pass'word'";

Other ways to cause errors
I have observed that the same error occurs with the -ConnectionString parameter as well as the -Credential parameter.
As far as I have tried in my environment, I think the same error occurs with Secret, but I don't know if the client secret of the service principal could contain single quotes. Does anyone know what the rule is for this?

Solution
I contacted MSFT with a support request, but they replied that they are not going to update the CrmServiceClient constructor.
To avoid the error, I think we need to update the logic of Connect-CrmOnline, if the password or connection string contains single quotes, to double the single quotes (escape) by replace function.

Password='Pass'word' → Password='Pass''word'

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions