Learn about Windows PowerShell
Summary: Guest blogger and Microsoft PFE Stefan Stranger talks about using version control to protect your Windows PowerShell scripts.
Microsoft Scripting Guy, Ed Wilson, is here. Today I am pleased to present a guest blog post written by Senior Premier Field Engineer Stefan Stranger. The Scripting Wife and I were privileged to hear Stefan speak at the first-ever Dutch Windows PowerShell user group meeting. Following Stefan’s presentation, I asked him if he would write up his presentation as a guest blog post. Today is the result of that request.
Take it away, Stefan ...
During the first Dutch PowerShell User Group meeting, I gave a presentation about protecting your Windows PowerShell scripts with version control, and this is a blog post about how you can get started implementing version control.
Software developers would not think about working on a project without some form of version control. So why is this so unusual for the IT professional who is responsible for maintaining Windows PowerShell scripts containing hundreds of lines of scripts? Version control or source control are terms used for the practice of tracking changes in the source code. There are several tools that can help you implement version control such as GIT, Mercurial, and Team Foundation Server. This blog post explains how you can protect your Windows PowerShell scripts by using Team Foundation Server (TFS) for version control. After implementing version control you will never have to worry that you did not copy your script after you made some changes in your script and it no longer works.
With version control, you can:
Other names used for version control are:
Microsoft Visual Studio Team Foundation Server 2012 (TFS) is the collaboration platform at the core of Microsoft's application lifecycle management (ALM) solution. TFS supports agile development practices, multiple IDEs and platforms locally or in the cloud, and it gives you the tools you need to effectively manage software development projects throughout the IT lifecycle.
Team Foundation Server consists of the following components:
When you want to start implementing version control, here are some basic commands you need to know:
Note There are many more commands, but this will get you started with some basic version control activities.
Microsoft offers Team Foundation Service, which is a cloud-based Team Foundation Service. At the moment, it is free for up to five users, and for a limited time, all use is free! You can also install TFS 2012 locally. For more information, see Team Foundation Server. There is also an Express version available to get started.
In this blog post, we are going to set up the following version control environment on a computer running Windows 8:
Start with the sign up.
After the initial setup, we need to create a Project. A Project is a repository for source code and work items.
You first need to download Team Explorer for Microsoft Visual Studio 2012 and run the installation.
Open Team Explorer, and click Connect to Team Foundation Server.
Enter the URL of your cloud-based Team Foundation Server.
Now it’s time to create a workspace mapping. Go to Source Control Explorer in Team Explorer and map your local drive. You can choose or create every folder you want.
We are going to use the Windows PowerShell cmdlets from the Microsoft Visual Studio Team Foundation Server 2012 Update 1 Power Tools to communicate from within Windows PowerShell with Team Foundation Server.
Notes
After downloading the Power Tools, you need to install the Windows PowerShell cmdlets.
After installing the prerequisites, you can get started with protecting your Windows PowerShell script with version control.
Open the Windows PowerShell ISE, and then load the Team Foundation cmdlets from the Visual Studio Team Foundation Server 2012 Power Tools.
Get-PSSnapin –Registered
Add-PSSnapin Microsoft.TeamFoundation.PowerShell
Check the commands that are available in the Microsoft.TeamFoundation.PowerShell snap in.
Get-Command –Module Microsoft.TeamFoundation.PowerShell
In this blog post, we are going to use the following cmdlets for controlling versions of your Windows PowerShell scripts.
#Check if Microsoft.TeamFoundation.PowerShell is installed
Get-PSSnapin -Registered
#Import Microsoft.TeamFoundation.PowerShell Snapin
Add-PSSnapin Microsoft.TeamFoundation.PowerShell -Verbose
#Connect to TFS Server
Get-TfsServer -Name "https://[name].visualstudio.com/defaultcollection"
#Create HelloWorld.ps1 file
New-Item C:\DEV\PowerShell\HelloWorld.ps1 -ItemType File -Force
#Add File
#Adds files and folders from a location in the local file system to a version control server for Team Foundation.
Add-TfsPendingChange -Add -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose
#Check in new file
New-TfsChangeset -Item C:\DEV\PowerShell\HelloWorld.ps1 -Comment "Initial Check in" -Verbose
#Check out for editing
#Makes the local file writable and changes its pending Change status to "edit" in the workspace. Edit is an alias for the Checkout command.
Add-TfsPendingChange -Edit -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose
#Get Pending change
#Displays information about pending changes to items in one or more workspaces.
Get-TfsPendingChange -Item C:\DEV\PowerShell\HelloWorld.ps1 -Verbose
#Make changes to C:\DEV\PowerShell\HelloWorld.ps1
Set-Content -Path C:\DEV\PowerShell\HelloWorld.ps1 -Value "Hello, World"
#Check in after making changes
New-TfsChangeset -Item C:\DEV\PowerShell\HelloWorld.ps1 -Comment "Added some extra script commands" -Verbose
#Get History
Get-TfsItemHistory -HistoryItem C:\DEV\PowerShell\HelloWorld.ps1 -Server (Get-TfsServer -name "https://[name].visualstudio.com/defaultcollection")
#Remove Pending Change
#Stores a set of pending changes, together with pending check-in notes, a comment, and a list of associated work items, on the version control server for Team Foundation without actually checking them in.
Remove-TfsPendingChange -Item C:\DEV\PowerShell\HelloWorld.ps1 -Workspace (Get-TfsWorkspace -Path C:\DEV\PowerShell)
Did you know that you can also load the TFS Client Assemblies to achieve the same as the Team Foundation cmdlets from the Visual Studio Team Foundation Server 2012 Power Tools?
# Load TFS Client Assembly
[Reflection.Assembly]::Load('Microsoft.TeamFoundation.VersionControl.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer("https://[name].visualstudio.com/defaultcollection")
#Create HelloWorld2.ps1 file
New-Item C:\DEV\PowerShell\HelloWorld2.ps1 -ItemType File -Force
$WorkstationType = [Microsoft.TeamFoundation.VersionControl.Client.Workstation]
$WorkspaceInfo = $WorkstationType::Current.GetLocalWorkspaceInfo("C:\DEV\PowerShell")
# Get Collection
$Collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($WorkspaceInfo.ServerUri)
$Collection.EnsureAuthenticated()
# Get Workspace. Handler for Workspace changes
$Global:Workspace = $WorkspaceInfo.GetWorkspace($Collection)
$Workspace.PendAdd("C:\DEV\PowerShell\HelloWorld2.ps1")
$pendingChanges = $workspace.GetPendingChanges() | Where-Object {$_.LocalItem -eq "C:\DEV\PowerShell\HelloWorld2.ps1"}
$Comment = "Initial version of the HelloWorld2.ps1 file"
$workspace.CheckIn($pendingChanges,$Comment)
$Workspace.PendEdit("C:\DEV\PowerShell\HelloWorld2.ps1")
$pendingChanges
#Make changes to C:\DEV\PowerShell\HelloWorld2.ps1
Set-Content -Path C:\DEV\PowerShell\HelloWorld2.ps1 -Value "Hello, World"
#Get Tfs Item property information
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($WorkspaceInfo.ServerUri)
$vcs = $tfs.GetService([Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]) #Version Control Server
$vcs.GetExtendedItems("C:\DEV\PowerShell\HelloWorld2.ps1", [Microsoft.TeamFoundation.VersionControl.Client.DeletedState]::NonDeleted, [Microsoft.TeamFoundation.VersionControl.Client.ItemType]::Any)
$vcs.QueryHistory("C:\DEV\PowerShell\HelloWorld2.ps1", [Microsoft.TeamFoundation.VersionControl.Client.VersionSpec]::Latest, 0, 'None', $null, $null, $null, 1, $false, $false)
Have fun using Windows PowerShell and TFS to protect your Windows PowerShell scripts with version control.
~Stefan
Thank you, Stefan. Join me tomorrow when I will present an excerpt from my much anticipated Windows PowerShell 3.0 Step by Step book.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
A well written post, Stefan! Just as good as the presentation you gave at DuPSUG... ;-)
<IMHO> Using the Shell Extensions is much more useful than PowerShell cmdlets.