• .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…

  • .NET Core,  dotNet,  Powershell,  VSCode

    Powershell Core binary module with Visual Studio Code

    I wrote countless scripts and a good amount of modules (and functions) in my years as Service Engineer but all of them are script modules, I never really created binary modules. The main reason is that I like to write in Powershell but I also like the fact that, not being a compiled language, it is very easy to share and modify the source code for a script module and it is immediately ready to be reloaded and used. Anyway out of curiosity and to learn a different approach to building modules, I decided to try to convert one of mine from script to binary; my first step was (of course) a quick search to find some samples and getting started articles and I found a few good ones (referenced below) but all of them use Visual Studio and the full version of the .NET Framework, while I want to use Visual Studio Code and .NET Core. So here’s what I came up with. First off of course I need .NET Core (I am using the latest .NET Core 3 preview 8 at the moment), Visual Studio Code and the C# Extension. Next, I’m going to create a Class Library…

  • .NET Core,  Powershell

    X509Certificate is immutable on this platform. Use the equivalent constructor instead

    Quick tip today. Recently I decided to switch to to Powershell Core as default on all my machines for my daily work and it’s working great (except a few corner cases where I’m forced to go back to Powershell Desktop due to some old module incompatibility). To do so, over the last few weeks I had to go through the modules and script I use the most and port them to Pwsh. One of my cmdlets is meant to convert a certificate to and from its Base64 representation (this is useful to export certificates from Azure KeyVault for example), the heart of the code where the transformation happens looks like this: Unfortunately though, while testing the code on Powershell Core I got this error: It turns out the problem is with how I was creating the certificate object and loading its data. To avoid the exception the solution is to go from this: To this: I didn’t spent too much time to figure out why the exception is thrown (especially considering the Import() method is available on .NET Core/Pwsh) but at least I hope this will save someone else some time (and a headache 😉). I have never met a man so ignorant that…