Using PnP.PowerShell in Visual Studio Development Containers
Visual Studio Code development containers allows you to work with PnP.PowerShell commands in containers that Visual Studio will run automatically for you so you don't need to care running docker run
. In this way it is also easier to make sure that all members of the project are using the same development environment, with the same versions of the prerequisites installed.
If you want to learn more about this Visual Studio Code feature, you can start here.
Prerequisites
There might be two options of using Visual Studio Code development containers with PnP.PowerShell:
- Using locally installed Docker, on your desktop/laptop.
- Running containers in cloud using GitHub CodeSpaces (requires a paid GitHub plan)
When you run in a development container in GitHub CodeSpaces, you do NOT need to install any version of the PnP.PowerShell directly on your laptop/desktop.
What you will need locally is only Visual Studio Code!
Locally Installed Docker
For using that option you need to have locally installed the following software:
- Visual Studio Code
- Visual Studio Code extension, Remote - Containers
- Docker Engine. Kindly use
Installing Docker locally
section of theUsing PnP PowerShell in Docker containers
article for installation instructions.
GitHub CodeSpaces
This option requires the following:
- Git repository in a GitHub organization with enabled CodeSpaces
Defining Development Containers for the Project
In the root folder of the source code, create the folder called
.devcontainer
.In this folder, create the
devcontainer.json
file with the following contents:
{
"name": "PnP 2.4.0",
"image": "m365pnp/powershell:2.4.0-alpine-3.16.5",
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.defaultProfile.linux": "pwsh"
},
"extensions": [
"ms-vscode.powershell"
]
}
},
"extensions": [
"editorconfig.editorconfig",
"dbaeumer.vscode-eslint",
"ms-vscode.PowerShell"
],
"postCreateCommand": "apk add git; git config --global --add safe.directory /workspaces"
}
Starting working with PnP.PowerShell when you run Docker locally
- Open the folder in Visual Studio Code
- Click in the bottom left corner:
- Select
Reopen in Container
:
- Open a new Visual Studio terminal and run
Connect-PnPOnline -Url https://<your-tenant-name>.sharepoint.com
The command should work even if you don't have PnP.PowerShell module installed locally on your laptop.
Starting working with PnP.PowerShell when you run containers remotely in GitHub CodeSpaces
- Open the project in GitHub site.
- Click
Code
button:
- Click
Create codespace on ...
button:
- Run
Connect-PnPOnline -Url https://<your-tenant-name>.sharepoint.com
in the Visual Studio Code terminal
The command should work even if you don't have PnP.PowerShell module installed locally on your laptop.