As you know the Office Dev PnP is always improved by many contributors. The latest code and setup is available on git. But sometimes you are receiving unexpected errors and dont know what is going wrong. In this situation you would like to investigate this issue. It is possible to debug the Office Dev PnP Powershell. See here below a tutorial how to do this in Visual Studio.
Before we start we need to uninstall Office Dev PnP from your local machine if it is already installed.
Download Office Dev PnP Powershell from git: https://github.com/SharePoint/PnP-PowerShell.
Unzip the downloaded file to some location on your machine. In my situation I unzip it to this location: C:\Temp.
In the next step we are going to download Office Dev PnP Core from git: https://github.com/SharePoint/PnP-Sites-Core.
Unzip the downloaded file in the same location as where you unzipped the previous zip file. In my situation I unzip it to this location: C:\Temp.
You will have now 2 seperated folders. One for PnP Powershell and the other for PnP Core.
Open the PnP Core solution in Visual Studio.
Build the PnP Core project.
The build will generate the PnP Core dll in the bin folder of this project. If you dont know where to find this bin folder go first to the project root folder.
In the project root folder you will find the bin folder.
Inside the bin folder you need to go to the debug folder. Inside this debug folder you will find the PnP Core DLL file.
Remember this location of the PnP Core DLL file. We need this location in another step.
Open the PnP Powershell solution in a second window of Visual Studio. Do not close the PnP Core solution in Visual Studio.
You will have at this moment 2 Visual Studio instances.
In the PnP Powershell solution in Visual Studio you will find 4 projects.
Build the project “SharePointPnP.PowerShell.CmdletHelpAttributes”.
Expand the references of project “SharePointPnP.PowerShell.Commands”.
You will see all the references which are needed for this project. When you scroll down a litle bit you will find missing references. You can see it by the warning icon.
We need to fix these missing references. For each missing reference you need to add it. Before we start adding the dll references you need to know where you can find these dll reference files.
You will find all the missing reference dll files in the folder “Assemblies” of the PnP Core folder. Inside this folder you need to open the folder “16.1” which is for O365.
In my situation it is this location: C:\Temp\PnP-Sites-Core-master\PnP-Sites-Core-master\Assemblies\16.1.
Right click on “References” and click on “Add Reference”.
Click in the left panel on “Browse” and then again on the other “Browse” button.
Go to the dll references location like we told you before. Select all the files in this folder and click on “Add”.
Now we are only missing 1 reference, and thats the reference to PnP Core. And thats the dll which we generated in the PnP Core solution by a build.
Add again a new reference and select the PnP Core dll file which you can find in the bin folder of the PnP Core solution.
These are the references you added. Click on Ok.
Build the project “SharePointPnP.PowerShell.Commands”.
Build the project “SharePointPnP.PowerShell.ModuleFilesGenerator”.
Because there is a post build event in the project “SharePointPnP.PowerShell.Commands”, there will be created a new subfolder in the folder “Documents”:
Inside this folder you will find the PnP Powershell module which used when you execute PnP Powershell commands.
Open the Powershell ISE console as administrator. Check if there is the PnP Powershell module.
Lets open the PnP Core solution in Visual Studio.
Add some breakpoints where you would like to debug. In my example I will set a breakpoint in the method “GetProvisioningTemplate” in class “WebExtensions.cs”. This method will be executed by the PnP Powershell command “Get-PnPProvisioningTemplate”.
Go to tab “Debug” and click on “Attach to process”.
Enable the checkbox “Show processes from all users”. And search on the word “powershell”. Select the powershell process and click on “Attach”.
Now we arrived to the last step where we are going to run some PnP Powershell command in the Powershell console. I connect to a site collection. And then I get the provisioning template.
Connect-PnPOnline -url https://yourtenant.sharepoint.com/sites/Khalid
Get-PnPProvisioningTemplate -Out ‘c:/myPnPProvisioningTemplate.xml’
When you run this PnP Powershell command the breakpoint will hit. Now you can debug in Visual Studio.