powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] Connect-PnPOnline from Azure Function does not work anymore

Open honb opened this issue 5 years ago • 9 comments

Notice

My command worked before, but now it dose not work anymore.

  1. 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

honb avatar Aug 24 '21 03:08 honb

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

AspenForester avatar Sep 30 '21 18:09 AspenForester

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.

bhavyachhabra29 avatar Oct 11 '21 19:10 bhavyachhabra29

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)

BaronSparky avatar Oct 12 '21 07:10 BaronSparky

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 -ClientId -ClientSecret

bhavyachhabra29 avatar Oct 12 '21 08:10 bhavyachhabra29

@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

honb avatar Oct 12 '21 09:10 honb

Is the issue still occurring ? Can you try with the latest version , preferably a nightly build and let us know ?

gautamdsheth avatar Dec 15 '21 06:12 gautamdsheth

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 avatar Dec 20 '21 07:12 mbuergi

@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

gautamdsheth avatar Dec 21 '21 05:12 gautamdsheth

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:

  1. Open PowerShell 7.2.1
  2. Use Connect-PNPOnline to connect to tenant, via certificate thumbprint.
  3. Success.
  4. Use Connect-ExchangeOnline to connect to tenant, via certificate thumbprint.
  5. Success.
  6. Exit.
  7. Open Powershell 7.2.1
  8. Use Connect-ExchangeOnline to connect to tenant, via certificate thumbprint.
  9. Success.
  10. Use Connect-PNPOnline to connect to tenant, via certificate thumbprint.
  11. Failure with the known error message in this bug report.

Figured I would add these findings in case it helps anyone with quick workaround.

Deweyoxberg avatar Jan 26 '22 00:01 Deweyoxberg