[BUG] Connect-PnPOnline from Azure Function does not work anymore
Notice
My command worked before, but now it dose not work anymore.
- Connect-PnPOnline -Url $siteUrl -ClientId $clientId -CertificateBase64Encoded $secretPlainText -Tenant $tenant
Expected behavior
Able to run above command without error
Actual behavior
Get error message: The system cannot find the file specified.
Steps to reproduce behavior
Execute that command in Azure Function PowerShell
What is the version of the Cmdlet module you are running?
Connect-PnPOnline (PnP.PowerShell 1.7)
Which operating system/environment are you running PnP PowerShell on?
- [ ] Windows
- [ ] Linux
- [ ] MacOS
- [ x] Azure Cloud Shell
- [x ] Azure Functions
- [ ] Other : please specify
I'm seeing this error pretty consistently too, from PowerShell 7.1.4, on a Win10 Machine.
Connect-PnPOnline: Could not load file or assembly 'Microsoft.Identity.Client, Version=4.30.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'. Could not find or load a specific file. (0x80131621)
Full exception:
Exception :
Type : System.IO.FileLoadException
Message : Could not load file or assembly 'Microsoft.Identity.Client, Version=4.30.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'.
Could not find or load a specific file. (0x80131621)
FileName : Microsoft.Identity.Client, Version=4.30.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae
TargetSite :
Name : CreateWithCert
DeclaringType : PnP.PowerShell.Commands.Base.PnPConnection
MemberType : Method
Module : PnP.PowerShell.dll
StackTrace :
at PnP.PowerShell.Commands.Base.PnPConnection.CreateWithCert(Uri url, String clientId, String tenant, String tenantAdminUrl, AzureEnvironment
azureEnvironment, X509Certificate2 certificate, Boolean certificateFromFile)
at PnP.PowerShell.Commands.Base.ConnectOnline.ConnectAppOnlyWithCertificate() in c:\PnPPowerShell\src\Commands\Base\ConnectOnline.cs:line 460
at PnP.PowerShell.Commands.Base.ConnectOnline.Connect(CancellationToken& cancellationToken) in
c:\PnPPowerShell\src\Commands\Base\ConnectOnline.cs:line 249
at PnP.PowerShell.Commands.Base.ConnectOnline.ProcessRecord() in c:\PnPPowerShell\src\Commands\Base\ConnectOnline.cs:line 208
at System.Management.Automation.CommandProcessor.ProcessRecord()
Data : System.Collections.ListDictionaryInternal
InnerException :
Type : System.IO.FileLoadException
Message : Could not load file or assembly 'Microsoft.Identity.Client, Version=4.30.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'.
TargetSite :
Name : LoadFromPath
DeclaringType : System.Runtime.Loader.AssemblyLoadContext
MemberType : Method
Module : System.Private.CoreLib.dll
StackTrace :
at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack
retAssembly)
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)
at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)
at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)
Source : System.Private.CoreLib
HResult : -2146232799
Source : PnP.PowerShell
HResult : -2146232799
CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], FileLoadException
FullyQualifiedErrorId : System.IO.FileLoadException,PnP.PowerShell.Commands.Base.ConnectOnline
InvocationInfo :
MyCommand : Connect-PnPOnline
ScriptLineNumber : 243
OffsetInLine : 23
HistoryId : 2
ScriptName : C:\Users\jole001\Source\OneDrive-DLP-ShareAudit\OneDrivePerms.ps1
Line : $Connection = Connect-PnPOnline -Url $siteUrl -Tenant $tenantId -Thumbprint $CertificateThumbprint -ClientId $clientId
-ReturnConnection -ErrorAction Stop
PositionMessage : At C:\Users\jole001\Source\OneDrive-DLP-ShareAudit\OneDrivePerms.ps1:243 char:23
+ … onnection = Connect-PnPOnline -Url $siteUrl -Tenant $tenantId -Thumbp …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:\Users\jole001\Source\OneDrive-DLP-ShareAudit
PSCommandPath : C:\Users\jole001\Source\OneDrive-DLP-ShareAudit\OneDrivePerms.ps1
InvocationName : Connect-PnPOnline
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, C:\Users\jole001\Source\OneDrive-DLP-ShareAudit\OneDrivePerms.ps1: line 243
This issue is occuring with Powershell 7. As a workaround, it works by loading the PnP.Powershell module using the Windows PowerShell Compatibility feature.
At the top of your azure function i.e. run.ps1 file, add this statement: Import-Module -Name PnP.Powershell -UseWindowsPowerShell
Thanks to Ian for providing this tip here: https://docs.microsoft.com/en-us/answers/questions/394459/getting-39could-not-load-file-or-assembly-39micros.html
However, it is consistently coming with my Azure function when it runs on Powershell 7 without this statement. A fix is needed here. In the requirements.psd1, the version for PnP.Powershell module is 1.* and the PnP.Powershell module version which was actually loaded was 1.8.0.
I have an Azure PowerShell function that makes use of:
Connect-PnPOnline -Url <site> -ClientId <clientId> -Thumbprint <thumbprint> -Tenant <tenant> -ErrorAction Stop
that is still working. (1.7.0)
Hi @BaronSparky , thanks for the update. Actually, I have noticed this yesterday that it is coming on some tenants, not all. I checked on 2 tenants yesterday. It was working on one but wasn't on the other with the same version of the module i.e. 1.8.0. I had tried using the client id and secret:
Connect-PnPOnline -Url
@bhavyachhabra29 Have you tried with the following case?
Azure function: PowerShell Core 7.0 and App service plan is B1 (not Consumption plan) Connect-PnPOnline -Url $siteUrl -ClientId $clientId -CertificateBase64Encoded $secretPlainText -Tenant $tenant ==> I got an error that "The system cannot find the file specified."
When I create a new function with app service plan is Y1 (Consumption) then it works
Is the issue still occurring ? Can you try with the latest version , preferably a nightly build and let us know ?
I performed some tests: With PnP.Powershell = 1.* in requirements.psd1 the connection with Base64Encoded certificat fails. With PnP.Powershell = 1.8.73-nightly the connection with Base64Encoded certificate is successfull.
AppSetting WEBSITE_LOAD_USER_PROFILE=1 is required for this to work.
@gautamdsheth Do you know when this will be included in a stable build?
@mbuergi - thanks for confirming the fix. We plan to release it as soon as possible, can’t give you an ETA for it unfortunately.
Closing the issue as it has been fixed in latest nightly builds
FYI, I have found that as of PNP 1.9.0 (Stable), there is an odd quirk depending on the order this module is loaded when using other modules in the same (session?instance?). For this test I have EXO V2 2.0.5 and PNP.Powershell 1.9.0.
Steps to reproduce:
- Open PowerShell 7.2.1
- Use Connect-PNPOnline to connect to tenant, via certificate thumbprint.
- Success.
- Use Connect-ExchangeOnline to connect to tenant, via certificate thumbprint.
- Success.
- Exit.
- Open Powershell 7.2.1
- Use Connect-ExchangeOnline to connect to tenant, via certificate thumbprint.
- Success.
- Use Connect-PNPOnline to connect to tenant, via certificate thumbprint.
- Failure with the known error message in this bug report.
Figured I would add these findings in case it helps anyone with quick workaround.