I have a confession to make; I have been very lax about version control in my PowerShell scripts. It is easy to view scripting from a sysadmin mindset. The script exists to fix a problem or process that existed. Then when that process changes just make appropriate changes to the script. Complications arise when you add other editors into the mix or a previous version of the script is needed. Version control has been long overdue for me as both of those situations have happened.
There are many solutions for version control and more than I will list here. Our applications team uses Team Foundation Server (TFS) and I chose to use that to leverage our in-house resources. The next decision is how to interact with TFS. Visual Studio is an obvious choice and the Community Edition is available for free. That is a fine solution, but a bit heavy handed for editing PowerShell scripts. The install takes up about 8 Gb of free space as well. Another option would be using PowerShell ISE. I use ISE quite often for editing and testing scripts and integration to TFS would be handy. I looked into Jason Stangroome’s profile script that automatically checks for Team Explorer then registers events for the scripts you have open and checks out files that you edit. That is a solid solution; however, it appears that for TFS 2015 only Team Explorer Everywhere was released and not Team Explorer.
I chose to proceed with Visual Studio (VS) Code. VS Code is much more lightweight than running a full version of Visual Studio. It retains intellisense like ISE (Integrated Scripting Environment) and has Git support built in. I can create a Git repository in TFS then connect VS Code to that repository and control the script. I have found this very easy and intuitive to work with. So how can someone getting started with version control set this up? I will walk you through how I did it.
Install VS Code
Once these are installed you can install the Visual Studio Team Services extension for VS Code. Press Ctrl + P to launch the quick open prompt. Type ext install team and then Enter to search for the extension.
That brings you to the extensions gallery. Select the Visual Studio Team Extensions and install. You can also install the PowerShell extension to enable PowerShell language support for VS Code while in the gallery.
The Team Services extension works by checking the remote origin of a Git repository. This tells it the TFS server to which it needs to connect. In order to be able to check the remote origin, the Git repository must be cloned locally.
Next, we can create a Git repository and clone it locally. Browse to your TFS web interface and select Administer.
Then click view collection administration page. Now we can create a new team project.
Fill in the appropriate information and change the Version Control drop down to Git.
Click Navigate to Project and that will let us into the project page. Click on code and it will give us the default code page. Since there is no code in this repository, it displays ways to connect.
Copy the Clone this repository command. Then create a folder to which you will clone the repository, and run the command in that path.
Then switch back to VS Code. Use Open Folder to select the repository you have cloned.
The repository folder is now open. To enter credentials to login to the TFS server use Ctrl + Shift + P to open the command prompt. Type team login. This will select the logged in user and prompt for a password.
Once that is accomplished, you should see the repository status in the lower left hand corner. This is VS Code showing that it is connected and providing information.
Now you can create a file. Select the repository name, click the new file icon, and name the file. I entered a comment and then saved my script.
You can then press Ctrl + Shift + G to jump to the Git page or click on the icon.
Right click on the script, and stage the changes
This does not show any changes since it will be the initial upload. For the next push there will be a comparison window highlighting the changes I have made. Type a message for the commit, and then press the check mark or use Ctrl + Enter.
Click the … button and select Push to.
Select the origin and push it.
Switching back to the TFS web page, visit the code page again, and see the script that I have pushed.
That is it! I was able to leverage our existing TFS server and use a lightweight client to connect and manage my scripts. If you have not already been using version control or needed a kick in the pants like I did, I hope this primer will help you get started.