.NET Core,  Powershell,  VSCode

Test Powershell on Linux with Visual Studio Code and Docker

One of the promises (maybe, the biggest promise) of .NET Core and Powershell Core is being cross-platform, be able to develop applications (or Powershell scripts and modules) that can be executed on Windows, Linux and macOS; but how can we be sure the our application or script/module will actually run properly across all those platforms and distros? Talking about Powershell, PSScriptAnalyzer helps to check a script or function compatibility with certain Powershell versions or different platforms. But what if I want to actually write and test my script/module on Linux (assuming my main machine is Windows, of course 😉)?

One easy solution is to spin up a virtual machine, install the tools I need (.NET Core, Powershell Core, Visual Studio Code and anything else required) or use the Remote Development extension pack for Visual Studio Code and use Docker containers instead. Note: this requires Visual Studio Code Insiders at this time; pay attention to the Installation notes for good tips to get started. Also, take a look at Developing Inside a Container in the official VSCode documentation.

The extension pack allows to choose between Windows Substrate for Linux, connect to a remote machine through SSH or run a Docker container on my machine. I want to use a container on my development machine:

connect to remote
connect to remote

Of course, Docker must be installed first (VSCode will remind you if it’s not 😅):

docker required
docker required

You’ll have to create an account on Docker Hub if you don’t already have one, then install Docker Desktop

docker desktop for windows
docker desktop for windows

I’m going to use the same binary Powershell module I showed in my previous post, so I’m going to use the command Remote-Containers: Open folder in container… from the Command Palette. The first time I’m opening the folder in a container I must choose a starting point, so for my test I’m going to use C# (.NET Core Latest)

choose container starting point
choose container starting point
C# (.NET Core Latest)
C# (.NET Core Latest)

By the way, there is a container for just Powershell, if you want to play with it:

container powershell
container powershell

Powershell Core (6.2.2 is the latest stable version at this time) is already installed under /root/.dotnet/tools/, I can just run pwsh to start it:

root@545230ed439e:/# find . -name pwsh*
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/preview/pwsh-preview.cmd
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.deps.json
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.runtimeconfig.json
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.dll
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.deps.json
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.runtimeconfig.json
./root/.dotnet/tools/.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.dll
./root/.dotnet/tools/pwsh
./root/.vscode-server-insiders/extensions/ms-vscode.powershell-2019.5.0/media/pwsh.svg
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/preview/pwsh-preview.cmd
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.deps.json
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.runtimeconfig.json
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/win/pwsh.dll
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.deps.json
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.runtimeconfig.json
./.store/powershell/6.2.2/powershell/6.2.2/tools/netcoreapp2.1/any/unix/pwsh.dll


root@545230ed439e:/# ./root/.dotnet/tools/pwsh
PowerShell 6.2.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.


PS /> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.2
PSEdition                      Core
GitCommitId                    6.2.2
OS                             Linux 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Now I can import and use the module as usual;


Don’t part with your illusions. When they are gone you may still exist, but you have ceased to live – Mark Twain

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.