Build Your Visual Studio Code Development Environment Using PowerShell DSC

I keep re-building my lab machines and in the process I rebuild my development virtual machines. One of the items that gets reinstalled all the time is Visual Studio Code editor. Every time I do this, I end up installing VS Code and all the required extensions manually which isn’t a good use of keystrokes and mouse clicks (if any!). I knew how to install VS Code editor from the command line but I wasn’t sure about the extensions. So, that is when I reached out to David Wilson asking if there is a way to install these extensions using command line.

David mentioned that this capability was coming to 1.2.0 and by evening I saw tweets from him that 1.2.0 is released. VS Code version 1.2.0 has the command line parameters for managing extensions.

  • code --list-extensions lists all installed extensions
  • code --install-extension installs an extension
  • code --uninstall-extension uninstalls an extension

So, I started writing a DSC resource module to wrap this functionality so that I can use a complete DSC-based method to build my development environment. A couple of hours later, I got the DSC resource module for VS Code. This is available on PowerShell Gallery as well.

This module contains a collection of DSC custom resources for installing VS Code and managing VS Code extensions.

At present, this DSC resource module includes 2 resources.

Before you can use any of these resources in a configuration script, you must first import the vscode module or a specific resource from this module.

1
2
3
Import-DscResource -Module vscode
Import-DscResource -Module vscode -Name vscodesetup
Import-DscResource -Name vscodeextension

Using vscodesetup resource

The vscodesetup resource can be used to install Microsoft Visual Studio Code editor.

When using this resource, both the IsSingleInstance and the Path must be specified. The IsSingleInstance can only have ‘Yes’ as a possible valid value. This is done to ensure that this resource gets used only once in the configuration document. The Path property takes the path to VS Code setup file. This can be downloaded from https://go.microsoft.com/fwlink/?LinkID=623230.

1
2
3
4
5
6
VSCodeSetup VSCodeSetup {
    IsSingleInstance = 'yes'
    Path = 'C:\temp\vscodesetup.exe'
    PsDscRunAsCredential = $Credential
    Ensure = 'Present'
}

The PsDscRunAsCredential is important because VS Code installation process creates the .vscode folder that stores all extensions under the logged-in user’s homepath. Without this, this folder gets created at the root of system drive. So, using_PsDscRunAsCredential_, you need to pass the current user credentials.

Using vscodeextension resource

The vscodeextension can be used to install new VS Code extensions from the marketplace. At this point in time, this relies on the command line provided by VS Code but I am exploring other alternatives. Therefore, only VS Code version 1.2.0 onwards is supported for installing VS Code extensions using this resource.

The only mandatory property in this resource is the Name property. You can use this to provide the name of the VS Code extension. Instead of dividing this into two properties like Publisher and Name, I decided to merge both of them into the _Name_property. Therefore the value to this property must be of the form Publisher.ExtensionName. You can find this from the marketplace URL for the extension. Using this method, you can be sure that you are always installing the right extension.

1
2
3
4
5
6
vscodeextension PowerShellExtension {
    Name = 'ms-vscode.PowerShell'
    PsDscRunAsCredential = $Credential
    Ensure = 'Present'
    DependsOn = '[vscodesetup]VSCodeSetup'
}

Like the vscodesetup resource configuration, this resource requires PsDscRunAsCredential to ensure the extension gets installed for the current user. Make a note that when using credentials in DSC configuration scripts, you must encrypt them using certificates. If the certificates cannot be deployed in a test or development environment, you can use the_PsDscAllowPlainTextPassword_ attribute in the DSC configuration data. Remember that this is not recommended in production environment.

Here is an example configuration document that installs VS Code and a couple of extensions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$ConfigurationData = @{
    AllNodes = 
    @(
        @{
            NodeName = '*'
            PSDscAllowPlainTextPassword = $true
        },
        @{
            NodeName = 'localhost'
        }
    )
}

Configuration VSCodeConfig {
    param (
        [pscredential] $Credential
    )
    Import-DscResource -ModuleName VSCode

    Node $AllNodes.NodeName {
        VSCodeSetup VSCodeSetup {
            IsSingleInstance = 'yes'
            Path = 'C:\temp\vscodesetup.exe'
            PsDscRunAsCredential = $Credential
            Ensure = 'Present'
        }

        vscodeextension PowerShellExtension {
            Name = 'ms-vscode.PowerShell'
            PsDscRunAsCredential = $Credential
            Ensure = 'Present'
            DependsOn = '[vscodesetup]VSCodeSetup'
        }

        vscodeextension CPPExtension {
            Name = 'ms-vscode.cpptools'
            PsDscRunAsCredential = $Credential
            Ensure = 'Present'
            DependsOn = '[vscodesetup]VSCodeSetup'
        }
    }
}

VSCodeConfig -ConfigurationData $ConfigurationData -Credential (Get-Credential)

TODO

There are certainly a few things I want to improve in this and also add more resources for customizing VS Code environment. I also want to explore if there is a better way to install extensions instead of using the command line provided with version 1.2.0.

Share on: