Search code examples
powershellpower-platformp-langxrm-tooling-connector

PowerShell Get-CrmConnection in XrmTooling is not working anymore


I have a piece of PowerShell code that generates CrmConnection. I tried this code 4-5 months ago and it did work. However, for some reason i am getting the below error while running it now. I can confirm $clientId, $clientSecret, $url are correct.

The code,

function New-PP-Connection
{
    param(
        [Parameter(Mandatory)] [string] $clientId,
        [Parameter(Mandatory)] [string] $clientSecret,
        [Parameter(Mandatory)] [string] $url
    )
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    $moduleName = "Microsoft.Xrm.Tooling.CrmConnector.PowerShell"
    $connectionString = "AuthType=ClientSecret;url=$url;ClientId=$clientId;ClientSecret=$clientSecret"

    if(-not(Get-Module -ListAvailable -Name $moduleName)){
        Install-Module $moduleName -Scope CurrentUser
    }
    Import-Module $moduleName

    write-host "Creating dynamics connection"
    $crmClient = Get-CrmConnection -ConnectionString $connectionString;
    write-host "Crm Client Created"  
}

Screenshot of the error enter image description here

error text,

Get-CrmConnection : Failed to connect to CRM: Invalid Login Information : The HTTP request is unauthorized with client authentication scheme 'Anonymous'

I have seen a similar issue in GitHub https://github.com/microsoft/coe-starter-kit/issues/2564 however no exact solution has been provided.


Solution

  • I may have come across a bug in Microsoft.Xrm.Tooling.CrmConnector.PowerShell where the above code only works if there is one secret per app registration. I had two secrets for this app registration. As soon as I removed one it started working.