简体   繁体   中英

Get-CrmConnection failing when executed from non-admin context (3.3.0.857)

I'm trying to execute Get-CrmConnection to connect to Dynamics 365 from Powershell, but the command returns the following when executed from a non-administrative Powershell context:

Get-CrmConnection -ConnectionString "AuthType=Office365;Username=xxx;Password=xxx;Url=https://xxx.crm.dynamics.com"

Get-CrmConnection : Failed to connect to CRM: Unable to Login to Dynamics CRM
At line:1 char:1
+ Get-CrmConnection -ConnectionString "AuthType=Office365;Username=crm_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Get-CrmConnection], Exception
    + FullyQualifiedErrorId : -10,Microsoft.Xrm.Tooling.CrmConnector.Powershell.Commands.GetCrmConnectionCommand

I have version 3.3.0.857 of the connector installed.

Additional Info

If I execute the same command within the context of an Administrator prompt, I'm able to connect without issue.

I need this command to work outside of an Administrator context because this script will be called as part of an Azure DevOps pipeline. When executing this code within the pipeline, hosted in Azure DevOps, the same issue occurs.

I was able to find a solution to my issue that doesn't involve elevating to Admin.

Using the -Verbose option when running Get-CrmConnection , I saw that the error that was occurring was that the connection was being forcibly closed by the host. After a little Googling, I ran across this forum post. The last post had a suggestion to try adding this to my script:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

I tried that and now everything works, admin or not.

You can run any script in elevated mode with the following:

$script = "-file C:\crmscript.ps1"
Start-Process powershell -Verb RunAs -ArgumentList $script

You can get valid verbs for any process using the System.Diagnostics.ProcessStartInfo object under the verbs property. Which, in this case, RunAs is a valid verb to start the powershell process in elevated mode.

You can then incorporate this into your Azure Pipelines/DevOps.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM