pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
990 stars 663 forks source link

Connect-PnPOnline : Could not load file or assembly NewtonSoft.Json.dll #2521

Closed SPNass closed 4 years ago

SPNass commented 4 years ago

Reporting an Issue or Missing Feature

Issue trying to connect SPO via Connect-PnPOnline

Expected behavior

I need to connect my SPO sites with PowerShell via an Azure Function App, to perform various operations such as managing my term store.

Actual behavior

I'm getting the following error message:

[Error] Connect-PnPOnline : Could not load file or assembly 'file:///D:\local\Temporary ASP.NET Files\root\c54b3d70\375e88f8\assembly\dl3\c98f7cad\83c7b496_74e6d501\NewtonSoft.Json.dll' or one of its dependencies. The system cannot find the file specified. at run.ps1: line 5

  • Connect-PnPOnline

    • CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], FileNotFoundException
    • FullyQualifiedErrorId : System.IO.FileNotFoundException,SharePointPnP.PowerShell.Commands.Base.ConnectOnline

image

I'm running under runtime 1 image

I included the parameter 'WEBSITE_LOAD_CERTIFICATES' to load certificates in my store image

The certificate used to connect SPO has been downloaded within the SSL store image

The module SharePointPnPPowerShellOnline version 3.18.2002.0 has been included within the function folder under modules folder. (Save-Module)

Steps to reproduce behavior

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

Connect-PnPOnline -Tenant $env:Tenant -ClientId $env:ClientId -Thumbprint $env:Thumbprint -Url $env:SiteUrl

New-PnPTerm -Name $name -TermSet $env:TermSetId -TermGroup $env:TermGroupId -Lcid 1033 -TermStore $env:TermStoreId 

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}

Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

Which version of the PnP-PowerShell Cmdlets are you using?

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

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable) SharePointPnPPowerShellOnline version 3.18.2002.0

How did you install the PnP-PowerShell Cmdlets?

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

tdwhite0 commented 4 years ago

@SPNass Same thing is happening to me. I am also using Azure Functions v1. I've always been able to make previous versions work, something about this one is different it seems.

tdwhite0 commented 4 years ago

I wonder if its the way it resolves assemblies in here being changed: https://github.com/SharePoint/PnP-PowerShell/commit/b2606edf73bf01c45d6adc78a2d5fa86cddf388a#diff-c3517ebd363d7ef8791f4eb9ae9626d4

SPNass commented 4 years ago

@tdwhite0 good to know I'm not alone facing this issue ☺️

tdwhite0 commented 4 years ago

@SPNass I did a local build and changed this back to the way it was, that fixes it for me: https://github.com/SharePoint/PnP-PowerShell/commit/b2606edf73bf01c45d6adc78a2d5fa86cddf388a#diff-c3517ebd363d7ef8791f4eb9ae9626d4

Looking at some of the other issues popping up with this release I think this could be the cause of multiple problems.

Changing it back would undo the original reason it was changed though: #2123

PowershellNinja commented 4 years ago

Seems likely this was caused by the latest change for the assembly loading. What PowerShell version is running under Azure Function v1.0? (If I remember correctly, v1.0 still used the full .NET Framework, right?)

SPNass commented 4 years ago

@PowershellNinja according this article, it's the .NET Framework 4.6. Running following command: Get-Host | Select-Object Version (from the Azure function app) I'm getting the version 5.1.14393.3383 which looks good.

SPNass commented 4 years ago

@PowershellNinja @tdwhite0 I confirm that using the previous intermediate version of PnP (3.17.2001.2), I don't have any issue related to assembly load. However, I had to change the way I'm authenticating as the parameter Thumbprint was not yet there.

rharbeg commented 4 years ago

@SPNass same here using SharePointPnPPowerShellOnline v 3.18.2002.0 and 3.9.1905.2 using function app @{Version=5.1.14393.3383}. On local Powershell (computer) works fine Version
5.1.18362.628.

any help or work around to make work on function app? Thanks

tdwhite0 commented 4 years ago

@rharbeg I couldn't find a workaround that would make the existing latest release work for me. The only thing I could make work was reverting this code: https://github.com/SharePoint/PnP-PowerShell/commit/b2606edf73bf01c45d6adc78a2d5fa86cddf388a#diff-c3517ebd363d7ef8791f4eb9ae9626d4, rebuilding PnP PowerShell, and using the custom build in my function.

PowershellNinja commented 4 years ago

This is actually strange, because I can‘t imagine that it is related to the powershell version as changes from 5.1 onwards have only been very minor bugfixes. I will have to investigate that with the functions app myself. For now, the workaround would be to use the last intermediate release with azure functions v1 (3.17.2001.2) then.

TawneyF commented 4 years ago

I am having this issue as well. If I load the module from a Function App v2, it does work - however I am not able to do this in my code because I have other modules that are reliant on .NET Framework from Function App v1 rather than the .NET Core from Function App v2.

I tried re-loading the module using both 3.17.2001.2 and 3.16.1912.0 but both result in error:

2020-02-21T10:16:30.196 [Info] Could not load file or assembly 'file:///D:\local\Temporary ASP.NET Files\root\fde359ce\fd7c8a81\assembly\dl3\447348e0\009407c9_5cdfd501\NewtonSoft.Json.dll' or one of its dependencies. The system cannot find the file specified.

Does anyone know of a workaround?

PowershellNinja commented 4 years ago

@SPNass @tdwhite0 @rharbeg I may have been able to pinpoint the issue - at least I was able to run PnP Powershell in a V1.0 Azure Function:

function

I still got some strange errors telling me that all the default PSDrives had already been created and could not be recreated, but PnP seemed to work despite those errors (are you getting these too?).

I am attaching a custom version of the module with my fix, could you please verify this works for you too? If it does, I'll open a pull request with my changes.

SharePointPnPPowerShellOnline.zip

TawneyF commented 4 years ago

I am having this issue as well. If I load the module from a Function App v2, it does work - however I am not able to do this in my code because I have other modules that are reliant on .NET Framework from Function App v1 rather than the .NET Core from Function App v2.

I tried re-loading the module using both 3.17.2001.2 and 3.16.1912.0 but both result in error:

2020-02-21T10:16:30.196 [Info] Could not load file or assembly 'file:///D:\local\Temporary ASP.NET Files\root\fde359ce\fd7c8a81\assembly\dl3\447348e0\009407c9_5cdfd501\NewtonSoft.Json.dll' or one of its dependencies. The system cannot find the file specified.

Does anyone know of a workaround?

I can confirm along with #2365#issuecomment-589665772 that this has "fixed itself". After trying all different module load outs from psgallery on my Function App v1 Powershell, that going home, having dinner, and then returning finds it is suddenly working.

SPNass commented 4 years ago

@SPNass @tdwhite0 @rharbeg I may have been able to pinpoint the issue - at least I was able to run PnP Powershell in a V1.0 Azure Function:

function

I still got some strange errors telling me that all the default PSDrives had already been created and could not be recreated, but PnP seemed to work despite those errors (are you getting these too?).

I am attaching a custom version of the module with my fix, could you please verify this works for you too? If it does, I'll open a pull request with my changes.

SharePointPnPPowerShellOnline.zip

@PowershellNinja In case, on Azure function v1, you don't need to run Import-Module command, as far as you have you have the module binaries within the modules folder. This is maybe what's causing the first errors.

I'll try your custom version and I'll let you know.

SPNass commented 4 years ago

@PowershellNinja I confirm this is better with your custom version, I can even use the Thumbprint certificate. However, I have the following errors:

2020-02-21T18:02:58.543 [Error] Import-Module : A drive with the name 'HKLM' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (HKLM:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:58.574 [Error] Import-Module : A drive with the name 'Alias' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (Alias:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:58.605 [Error] Import-Module : A drive with the name 'Env' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (Env:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:58.652 [Error] Import-Module : A drive with the name 'C' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (C:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:58.683 [Error] Import-Module : A drive with the name 'Function' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (Function:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:58.715 [Error] Import-Module : A drive with the name 'Variable' already exists.

  • Import-Module

    • CategoryInfo : ResourceExists: (Variable:String) [Import-Module], ProviderInvocationException
    • FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.ImportModuleCommand

2020-02-21T18:02:59.089 [Error] Connect-PnPOnline : Value cannot be null. Parameter name: certificate at run.ps1: line 10

  • Connect-PnPOnline

    • CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], ArgumentNullException
    • FullyQualifiedErrorId : System.ArgumentNullException,SharePointPnP.PowerShell.Commands.Base.ConnectOnline

2020-02-21T18:03:00.299 [Error] Exception while executing function: Functions.HttpTriggerPowerShell_AddSPOTerm_CAT. Microsoft.Azure.WebJobs.Script: PowerShell script error. System.Management.Automation: A drive with the name 'HKLM' already exists. A drive with the name 'HKLM' already exists.

2020-02-21T18:03:00.315 [Error] Function completed (Failure, Id=6bac97a3-4eec-4449-a3c0-0e41e3562884, Duration=2737ms)

PowershellNinja commented 4 years ago

@SPNass Thanks for confirming, I'll create a pull request. These additional errors may come from the fact that I built the custom module on the dev branch - but I'll verify that it doesn't happen with the master branch code.

Edit: Happens with the master branch code too. Still, seems to be unrelated to this issue, if it turns out to be blocking it would be best to create a separate issue for this.

airmnichols commented 4 years ago

I am having the same problem trying to use the module on PowerShell core on Linux.

Connect-PnPOnline : Could not load file or assembly '/root/.local/share/powershell/Modules/SharePointPnPPowerShellOnline/3.18.2002.0/NewtonSoft.Json.dll'. The system cannot find the file specified. At line:1 char:1

JCorrea13 commented 4 years ago

I am having the same problem trying to use the module on PowerShell core on Linux.

Connect-PnPOnline : Could not load file or assembly '/root/.local/share/powershell/Modules/SharePointPnPPowerShellOnline/3.18.2002.0/NewtonSoft.Json.dll'. The system cannot find the file specified. At line:1 char:1

  • Connect-PnPOnline -Url $site -Credential $cred
  • CategoryInfo : NotSpecified: (:) [Connect-PnPOnline], FileNotFoundException
  • FullyQualifiedErrorId : System.IO.FileNotFoundException,SharePointPnP.PowerShell.Commands.Base.ConnectOnline

I got the same error with Ubuntu, I think the problem is a mismatch with names files.

The filename when installing the module is 'Newtonsoft.Json.dll' but the command looks for 'NewtonSoft.Json.dll'. As Linux is case sensitive, it cannot find the file.

I just rename the file from 'Newtonsoft.Json.dll' to 'NewtonSoft.Json.dll'. It worked for me.

move /root/.local/share/powershell/Modules/SharePointPnPPowerShellOnline/3.18.2002.0/Newtonsoft.Json.dll /root/.local/share/powershell/Modules/SharePointPnPPowerShellOnline/3.18.2002.0/NewtonSoft.Json.dll

PaoloPia commented 4 years ago

Hi, Thanks for sharing the issue. We're already working on fixing it, in the meantime please rely on PnP PowerShell version 3.17.2001.2 if you need to use it in a PowerShell Azure Function (v1). Thanks.

KoenZomers commented 4 years ago

Should be fixed by PR #2566 which hopefully makes it into the May 2020 release next week.

jackpoz commented 4 years ago

This issue has been fixed in the March 2020 release https://github.com/pnp/PnP-PowerShell/releases/tag/3.19.2003.0

The February 2020 release of PnP PowerShell was throwing an error on not being able to find and load the Newtonsoft assembly when used in an Azure Function. Fixed in this release.

Please note that in meantime the April 2020 release has been released, so grab the latest version to fix the issue 😃

KoenZomers commented 4 years ago

Should be fixed by PR #2566 which hopefully makes it into the May 2020 release next week.

Sorry, meant to say March 2020 release. It's fixed in the last two releases already indeed :)

KoenZomers commented 4 years ago

Closing this issue. If you still have problems, feel free to reopen.