MSAL.PS icon indicating copy to clipboard operation
MSAL.PS copied to clipboard

New-MsalClientApplication : Cannot convert argument "builder"

Open JorgaWetzel opened this issue 2 years ago • 4 comments

Hello,

I was able to successfully send this command:

Connect-MgGraph $Current_MgContext = Get-MgContext $Global:AccessToken = Get-MsalToken -ClientID $Current_MgContext.ClientId -TenantId $Current_MgContext.TenantId

but then suddenly i have this persistent error:

New-MsalClientApplication : Cannot convert argument "builder", with value: "Microsoft.Identity.Client.PublicClientApplicationBuilder", for "WithDesktopFeatures" to type "Microsoft.Identity.Client.PublicClientApplicationBuilder": "Cannot convert the "Microsoft.Identity.Client.PublicClientApplicationBuilder" value of type "Microsoft.Identity.Client.PublicClientApplicationBuilder" to type "Microsoft.Identity.Client.PublicClientApplicationBuilder"." At C:\Program Files\WindowsPowerShell\Modules\MSAL.PS\4.37.0.0\Select-MsalClientApplication.ps1:56 char:29

  • ... plication = New-MsalClientApplication -ErrorAction Stop @paramNewMsal ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [New-MsalClientApplication], MethodException
    • FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument,New-MsalClientApplication

But this error is machine dependent, on another PC it runs fine.

JorgaWetzel avatar May 02 '23 14:05 JorgaWetzel

same problem ... any suggestions please?

ramonrub avatar Jun 05 '23 13:06 ramonrub

same problem, MSAL.ps version 4.35.1.3

cgyim avatar Jun 14 '23 07:06 cgyim

The explanation is simple. I have no idea about a structural solution that will keep working. The workaround is easy: import-module msal.ps import-module microsoft.graph.authentication [Microsoft.Identity.Client.PublicClientApplicationBuilder].Assembly

GAC Version Location

False v4.0.30319 C:\Program Files\WindowsPowerShell\Modules\msal.ps\4.37.0.0\Microsoft.Identity.Client.4.37.0\net45\Microsoft.Identity.Client.dll => It works.

import-module microsoft.graph.authentication import-module msal.ps [Microsoft.Identity.Client.PublicClientApplicationBuilder].Assembly

GAC Version Location

False v4.0.30319 C:\Program Files\WindowsPowerShell\Modules\microsoft.graph.authentication\1.28.0\Dependencies\Desktop\Microsoft.Identity.Client.dll => It doesn't work. This is an older version.

Here it appears to work by simply switching the order. I don't know if there are any side effects.

Some indications of better solutions are in here. (Basically it says "don't run both assemblies in same process".) https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.3

PascalVerbruggen avatar Jun 22 '23 09:06 PascalVerbruggen

Had the same issue; in my case it looks like it is caused when using webview2 for interactive login. Adding the lines below to my script (i.e. not changing the module code) fixed the issue so the popup browser window for auth. is displayed. Not pretty...suggest that an option is added to allow disabling use of webview2.

import-module msal.ps import-module microsoft.graph.authentication $r=Get-Module -Name msal.ps $t=$r.SessionState.PSVariable.GetValue('ModuleFeatureSupport',$null) $t.WebView2Support=$false Get-MsalToken -ClientId ............

RuneHL avatar Sep 21 '23 12:09 RuneHL