Setting up Visual Studio Code, GitHub and code signing certificate

Hi all,

As many of you probably know I do a lot of PowerShell scripting when time allows it, and after scripting, for many years I finally saw the need for source control. Source control not only ensures that I can revert back to a previous version of my code, but it also provides me with a single place to find all my code. I am using source control for both my personal work (like Citrixlab.dk and helping others out) and also for my professional work where I use a shared repository with my colleagues.

For my personal code, I actually have two source control systems, one is the public one for my sharing, this is GitHub, and the other repository is in Azure DevOps. Both work well, but I like to have the separate so that I have the code I am working on in my private repository and my published in the public one. I know I can use just one and mark the code as private or public, but I like the fact that they are separate.

So in this article, I will guide you through the setup of Visual Studio Code, GitHub and finally how to use a code signing certificate. The process is fairly simple, but I will do this guide anyway and hopefully do another blog post on more extended use of Visual Studio Code and GitHub.

Let’s get started.

Installing Visual Studio Code

Go to this website and download Visual Studio Code: https://code.visualstudio.com
2019-03-10_13-29-16.png

Unblock the downloaded file by right-clicking the file, click “Properties”
2019-03-10_13-30-41.png
Now mark the “Unblock” checkbox and hit “OK”
2019-03-10_13-31-03.png

Run the file to start the installation process. Hit “Next”2019-03-10_13-32-34.png

Accept the EULA and click “Next”
2019-03-10_13-33-07

Choose the installation directory. I have chosen to use the default location. Click “Next” to continue
2019-03-10_13-33-28

Name the start menu folder. I have chosen to use the default. Click “Next” 2019-03-10_13-33-48

Choose the additional tasks that fit your needs. I have chosen to mark all the options under “Other” this is because I use Code as my default edition. Click “Next”
2019-03-10_13-34-15

Review the selected configuration and click “Install” if you are satisfied
2019-03-10_13-34-34

The installer will do its work.
2019-03-10_13-34-51

When the installation is completed make sure that the checkbox is marked so that Code will open up so we can do some work in there. Click “Finish” to complete.
2019-03-10_13-35-01

When Code is open I want to install some extensions, this is done by clicking on the icon marked on the picture.

2019-03-10_13-35-37

Now search for “PowerShell” as shown and click on “Install”
2019-03-10_13-36-03
Next search for “Bracket” and click on “Install”. This is just an extension that I like to use so you can skip it if you don’t want it.
2019-03-10_13-36-50

Now close Visual Studio Code so we can move on to installing “Git for Windows”

Installing Git for Windows

Git for Windows is needed to be able to fetch and push code into GitHub. To install this we first need to download it and you can get it from this URL: https://code.visualstudio.com
2019-03-10_13-37-18.png

After the download completes perform the same unblocking steps as we did in the section about Visual Studio Code. Now right click the file and choose “Run as administrator” and click “Yes” to run the file
2019-03-10_13-37-46 2019-03-10_13-38-05

For the installation, I choose all the defaults, but let’s take it one step at the time. Click “Next” on the public license information.
2019-03-10_13-38-18
Choose the installation folder you want to use
2019-03-10_13-38-31

Choose the components, I haven’t changed anything on the selection as shown below. Click “Next”
2019-03-10_13-38-47

Name the folder in the Start menu and click “Next”2019-03-10_13-39-04

Choose the default edition to be used and click on “Next”
2019-03-10_13-39-21

Select the adjustments to the Path environment and click “Next”
2019-03-10_13-39-39

Choose the HTTPS transport backend and click “Next”
2019-03-10_13-39-56

Select the checkout style and click “Next”
2019-03-10_13-40-11

Choose the terminal emulator and click on “Next”
2019-03-10_13-40-29

Make your selection for extra options and click “Install”
2019-03-10_13-40-44

Let the installer do its magic
2019-03-10_13-41-02

Clear the checkboxes and click on “Finish”
2019-03-10_13-41-28

Now we are all set to make use of Git inside of our Visual Studio Code application.

Setting up GitHub and using it in Visual Studio Code

In this section, I will show you how to make a repository in GitHub then how to use that repository inside of Visual Studio Code.

The first thing we need to do is go to https://www.github.com and if you don’t have a user create this. I already have my user so I will just sign in.

2019-03-10_13-46-27

I will sign in using my username and password and then click “Sign in”
2019-03-10_13-46-53

On the left side go under “Repositories” and click on “New”
2019-03-10_13-48-13

I will fill out the fields as shown below calling my new repository “Citrixlab-Demo” and finish off by clicking “Create repository”
2019-03-10_13-49-19

After clicking “Create repository” I will get to a page showing me the URL on the repository and I will use this in Visual Studio Code to clone the repository.
2019-03-10_13-49-58

Open Visual Studio Code again, click on “View” and then “Command Palette” or press Ctrl+Shift+P.
2019-03-10_13-51-15

Type in “Git: Clone” and press “Enter”
2019-03-10_13-51-57

Now enter the URL of the Git repository that was created before and press “Enter”
2019-03-10_13-52-22

I have created a folder on my C-drive called “Git” which will hold all my repositories. A subfolder will be created by the Git provider so the folder containing the actual code files will be called “Citrixlab-Demo” just like my repository.
2019-03-10_13-52-50

When finished cloning the repository Visual Studio Code will ask you if you want to open the repository. Click on “Open Repository” to do this.
2019-03-10_13-53-15

Since my repository is empty there are no files to show, but this is what it will look like.
2019-03-10_13-54-13

So let’s create the first file by clicking on “File” and then “New File” or just press Ctrl+N
2019-03-10_13-55-02

I wrote a small PowerShell code snippet as shown below, and as you can see the file is not yet named or saved.
2019-03-10_13-55-49

So click on “File” and then on “Save” or press Ctrl+S
2019-03-10_13-56-09

Choose the “PowerShell” as the extension and name your file. I have chosen the name “Test.ps1” as my file name. Click on “Save”
2019-03-10_13-56-40

Now that the file is saved and Visual Studio Code knows that it is a PowerShell file, you will see that the coding window now colorized the code so it is pretty to look at.
2019-03-10_13-56-59

On the left side click on the icon as shown below. This is the source control part of Visual Studio Code.
2019-03-10_21-15-30.png

Provide a meaningful text for the reason you are pushing code to the source control (GitHub). I always start with “Initial push” and all the following will have a text saying what has changed since last push. Click on the check mark when you want to commit the change.
2019-03-10_13-57-54

If you haven’t staged your changes you will be asked the question below. Click on “Yes” to stage the changes.
2019-03-10_13-58-09

Now you would probably think that everything is good and your code is safely pushed to the repository, but that is actually not the case. You will need to tell Visual Studio Code to do the push and that is done by click on the three “…” in the corner and then click on “Push”
2019-03-10_13-58-36

Since we are using GitHub it will ask you for your credentials, so type them in and click on “Login”
2019-03-10_13-59-03

When the code is pushed to the repository you will be asked if Visual Studio Code should automatically run a “Git fetch” periodically. Answer “Yes” to this question.
2019-03-10_13-59-27

Now the code is pushed to the repository, so let’s go back to the GitHub website and see if it has been updated. If you click on the “Code” section in the top you should see that “Test.ps1” is now in the online repository.
2019-03-10_13-59-56

Now we have a fully working setup using Visual Studio Code, Git for Windows and GitHub as the repository. This is essential for moving on with keeping your code safe and versioned. You can create all the files you need and you can also create subfolders so for instance, you can create a subfolder called “Tests” which contains all your Pester tests for your code. We are now ready to move on to the code signing part that requires a certificate.

Signing your PowerShell code with a code signing certificate.

To perform this part of the guide you need to have a code signing certificate installed on your machine, if you don’t have one you can buy one at https://www.digicert.com/code-signing the service at digicert is great!

When you have it installed the code signing is very easy. Lets take a look at which certificates is available for code signing on your PC, to do this open Visual Studio Code, go to the terminal and type in “Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert”. It should look like the screenshot below
2019-03-15_18-11-49.png

To sign your PowerShell script we need to put the certificate into a variable so that we can use it afterwards. In the script section put the following in line 1 “$cert=Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert”. On line 2 we type in the code that actually signs the PowerShell script, so type in the following “Set-AuthenticodeSignature -FilePath C:\GIT\Citrixlab-Demo\test.ps1 -Certificate $cert”. When those two lines are written we just need to execute the code by pressing F5. This should give you the output below.
2019-03-15_18-15-23

If we now open our test.ps1 file it should look like this.
2019-03-15_18-17-20.png

If we check our test.ps1 script for code signing now with this command “Get-AuthenticodeSignature .\test.ps1” we should get something similar to the picture below.
2019-03-15_18-20-02.png

Summary

So this became quite a long guide, but I feel it is important to go through all these steps first so that when we get into more complex setups with coding in Visual Studio Code and PowerShell we have a common understanding of the setup of the system. What we have now is a fully functional setup with Visual Studio Code, GitHub source control and a code signing certificate so that we can sign the code we publish to GitHub or maybe even to the PowerShell gallery.

I hope this guide is of value to you, and as always reach out if you have feedback on the article. If i missed anything let me know.

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.