powershell icon indicating copy to clipboard operation
powershell copied to clipboard

Connect-PnPOnline: Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.12.2.0

Open sadomovalex opened this issue 3 years ago • 2 comments

Trying to use PnP.PowerShell 1.11.0 together with Az 8.0.1 module in PowerShell 7 on Windows 10:

Import-Module Az -Version 8.1.0
Import-Module PnP.PowerShell -Version 1.11.0
Connect-PnPOnline -Url https://{tenant}.sharepoint.com

Expected behavior

Successful connect to the target tenant

Actual behavior

The following error is shown: Connect-PnPOnline: Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.12.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific file. (0x80131621)

NOTE: if import PnP.PowerShell with "-UseWindowsPowerShell" param it starts to work.

What is the version of the Cmdlet module you are running?

PnP.PowerShell 1.11.0 / Az 8.0.1

the same behavior is reproduced with PnP.PowerShell 1.9.0 / Az 7.3.0

Which operating system/environment are you running PnP PowerShell on?

  • [ x] Windows

sadomovalex avatar Jul 11 '22 08:07 sadomovalex

Mixing different PowerShell modules in 1 session never works well due to version dependencies. There's not a whole lot we can do about it. Erwin van Hunen has once created an article on how to work around this with scriptblocks. Far from ideal, but one of the few options you have if you need to combine two modules in one PowerShell session. Let me see if I can find it back.

KoenZomers avatar Jul 11 '22 09:07 KoenZomers

Ok, if you may send link to this page please. Note also that in PowerShell 5.1 the same code works

sadomovalex avatar Jul 11 '22 10:07 sadomovalex

The construct can be found in this article: https://pnp.github.io/powershell/articles/azurefunctions.html

Read the important information box at the top, samples are available throughout the article.

Studermarc avatar Aug 28 '22 21:08 Studermarc

This is a huge pain since ThreadJobs introduce some limitations such as, you cannot share variables, you cannot do any user user interactions so any command with MFA such as Register-PnPAzureApp or Connect-PnPOnline -Interactive will not work. Say you are writing a script for provisioning related Office 365 and Azure resources using PnP.PowerShell and Az.

gabbsmo avatar Sep 12 '23 11:09 gabbsmo