question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scripts using PNP.PowerShell do not work in VSCode

See original GitHub issue

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.

Summary

I’ve installed PNP.PowerShell-Module Version 1.7.0. When working within a normal PowerShell, all CMDLets work fine - for example running “Get-PBPListItem” returns the expected items.

When I work in VSCode with installed PowerShell Extension (v 2021.8.0), ALL PNP-PowerShell CMDLets throw an exception (TargetInvocationException).

This used to work perfect earlier …

PowerShell Version

Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Visual Studio Code Version

1.59.0
379476f0e13988d90fab105c5c19e7abc8b1dea8
x64

Extension Version

ms-vscode.powershell@2021.5.1
ms-vscode.powershell@2021.8.0
ms-vscode.powershell-preview@2021.8.0

Steps to Reproduce

  • Install PNP.PowerShell module
  • run a CMDLet like Get-PNListItem in a regular powershell window -> working !
  • run a CMDLet like Get-PNListItem in a vscode powershell window -> TargetInvocationException

Visuals

image

Logs

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:46 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
andschwacommented, Oct 5, 2021

Re-reading through this whole issue, please correct me if I’m wrong, but I believe these problems are only occurring with Windows PowerShell (e.g. PowerShell 5.1), not PowerShell Core (e.g. PowerShell 7). This makes some sense too given that it’s an assembly loading issue and the ALCs used to separate dependencies from each other are no present in .NET Framework (which Windows PowerShell is bound to). So Windows PowerShell is loading the library from the GAC, and then when PnP.PowerShell needs a different version of the library, it cannot get it due to the fundamental nature of .NET Framework’s assembly loading logic and Global Assembly Cache.

Because VS Code PowerShell Editor already loaded their dependencies and has a ALC which does not check the requested dependency’s version, it will return the already loaded version. So in the case of the example, when our code at some point requests Microsoft.BCL.AsyncInterfaces 1.1.1, we get 5.0.0.

This is almost correct, except we tested that in https://github.com/PowerShell/vscode-powershell/issues/3510#issuecomment-904998935. This points to the issue being similar, but what I just outlined: the ALC logic is being avoided entirely due to this being an issue with Windows PowerShell 5.1 which uses .NET Framework and the GAC. The assembly resolve event handler in Windows PowerShell loads a version of Microsoft.BCL.AsyncInterfaces, and then because of the GAC, this is later returned to PnP.PowerShell. I’m unsure if there’s a way around this, because the way around it is to use Assembly Load Contexts, but that’s unavailable in this scenario.

1reaction
KoenZomerscommented, Dec 17, 2021

We have discussed this, but turns out its very complex to resolve, if possible at all. In all honesty, with everything we have open and the limited resources we have contributing to the code at the moment, I would not count on this to be fixed soon if at all.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to Use SharePoint PNP PowerShell in VSCode
Here is how I fixed my issue: 1. In VSCode open the Command Palette on Windows or Linux with Ctrl+Shift+P. On macOS, use...
Read more >
PowerShell editing with Visual Studio Code
json file by using Preferences: Open Settings (JSON) command from the Command Palette (Ctrl+Shift+P) or by changing the default settings editor ...
Read more >
Using Visual Studio Code for PowerShell Development
Launch the VS Code app by typing code in a console or code-insiders if you installed Visual Studio Code Insiders. Launch Quick Open...
Read more >
Using PnP.PowerShell in Visual Studio Code
Some cmdlets may run just fine with PS5, but don't be fooled: you want PS7.) Installing PowerShell on Windows – PowerShell | Microsoft...
Read more >
Introduction to CLI for Microsoft 365 and PnP PowerShell VS ...
In this 15-minute developer-focused demo, Adam Wójcik's extensions deliver relevant code snippets, documentation, and script samples for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found