Day 65: Integrating Git Source Control and PowerShell Authoring to Visual Studio 2013

Welcome to Day 65 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC. If this is the first time you are viewing an article of the series and you are new to DevOps and PowerShell Desired State Configuration (DSC), I encourage you to read through the previous installments, particularly Day 1. I can’t believe we are already in the second half of our 100 days! As we progress, expect us to push further into new concepts, as well we to tie together our work from previous days.

Objective of this post…

In Day 63, I introduced you to the open source powerhouse Git for source control of our PowerShell DSC projects. I want to build on this introduction to show you a fully-integrated PowerShell authoring experience with integrated version control, all within the Visual Studio console. Visual Studio 2013 offers full integration of Git and with the right add-in, you can even author and test PowerShell scripts within the Visual Studio UI. Mark my words, Visual Studio will be the go-to interface for DevOps. IT Pros should start getting comfortable now.

It is true that Visual Studio is not really PowerShell friendly by default today. However, there is a great (and free) add-on called PowerShell Tools for Visual Studio 2013 available for download from the TechNet Gallery that fixes that problem very effectively. The add-in provides key PowerShell authoring features like:

  • Intellisense
  • Debug capabilities
  • Test runner that returns output in a window within the Visual Studio UI.
  • PowerShell Script and Module project templates

Download PowerShell Tools for Visual Studio 2013 from the gallery at https://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62-6eee37294597

image_thumb23

 

Installing the PowerShell Add-in

The solution you will download at the link above comes as a single .vsix (Visual Studio extension installer) file.

On any computer with Visual Studio 2013 installed, simply right click the the file and select Open to install.

image_thumb1

 

Using the PowerShell Add-in

To see what the add-in brings to the party, launch Visual Studio and select  File –> New –> Project.

In the New Project window, under Templates, you will find a PowerShell node.

You will find two project templates: one for PowerShell Module and one for PowerShell Script.

For purposes of our discussion, I will select PowerShell Script Project.

Before you click OK, check the box labeled Add to source control.

SNAGHTML511aa45_thumb1

 

Integrating Git with Visual Studio

When you click OK button above, if you have never chosen your source control solution, you will be prompted to make your choice. Following our path started in Day 63, I will choose Git.

SNAGHTML515aa9f_thumb1

You can create a new Git repository or add your solution to an existing repository. From this point, you will work with your repositories from within the Visual Studio interface, rather than the Git GUI or Git Bash command line.

You can see your uncommitted solution by selecting View –> Team Explorer in the Visual Studio UI.
image_thumb6

Let’s move on and create and test a script in our solution.

 

Creating Scripts in your Solution

To get started authoring, in my Visual Studio console I will select View –> Solution Explorer.

Right click your selection and Select Add –> New Item.

image_thumb16

 

You will find options for PowerShell script, data file and script module (shown below). Pick PowerShell Script, provide a name and click Add.

SNAGHTML553af64_thumb1

Let’s write and test a PowerShell Script all within Visual Studio.

Write or paste a PowerShell script into the interface. Grab a script you’re already written or write something simple, just for testing.

Testing Your PowerShell Scripts within Visual Studio

Remember, the PowerShell Tools for Visual Studio 2013 add-in brings intellisense and pretty formatting (both shown below), so the script authoring experience is very similar to the PowerShell ISE, except we have access to our source control tool (Git) as well! We actually have access to even more than that, but we will focus on PowerShell and source control in this article.

image_thumb[6]

Right click in the script window and select Execute as script. Notice the additional options you have on this menu.

image_thumb[8]

Your script output is shown in the Output window below your script in the Visual Studio console.

image_thumb[4]

When you are done with your script (or scripts) in the project, it is time to commit your change.

 

Committing Your Changes

When you are done, you can right click your script and from the Source Control menu, select Commit to commit your work to your Git repository.

image_thumb[3]

You can even sync your repository to a remote server, including GitHub, from within the Visual Studio console.

image

If you want to look at your repositories through the native , you can still launch the native Git for Windows tools.

image_thumb[9]

Conclusion

That’s all for this installment. I hope this post has provided you with the tools to drive your PowerShell authoring and source control from Visual Studio.

Previous Installments

To see the previous installments in this series, visit “100 Days of DevOps with PowerShell”.

Leave a Reply

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