Check a file into Team Foundation Server with PowerShell
Posted by: Andy Schneider in Powershell, Team Foundation ServerWe have recently started using Microsoft’s Team Foundation Server for our projects. We are using SCRUM for our infrastructure projects (which has been an interesting learning endeavor and quite possibly deserves a blog post of its own sometime in the future)
Anyway, on the infra side, we typically don’t use source control. We use TFS mostly to manage our tasks for each sprint and our backlog items. However, we are starting to use PowerShell more and more in our build guides and giving our ops team PowerShell Scripts for deployments. Its only a matter of time until we will become just as dependent on Source Control as our development team is.
So I figured I would check into this. TFS actually has quite a nice little API that you can use with any .NET language, and so I started looking to see if I could use PowerShell to check in a script. Looks like its not that hard.
You need to install the Visual Studio 2008 SDK to get at the TFS assemblies, but that is not too hard.
In the SDK there are a few example solutions written in C# that show you how the basics for interacting with the API. I used the “VersionControlExample.cs” file and worked on re-writing some of it in PowerShell.
So here is my first crack at New-TFSItem
1: param (
2: [string]$tfsServer = "TFSServerName",
3: [string]$tfsLocation = "$/TFS/Project",
4: [string]$localFolder ="c:\scripts",
5: [string]$file,
6: [string]$checkInComments = "Checked in from PowerShell"
7: )
8: $clientDll = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.Client.dll"
9: $versionControlClientDll = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.VersionControl.Client.dll"
10: $versionControlCommonDll = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.VersionControl.Common.dll"
11:
12: #Load the Assemblies
13: [Reflection.Assembly]::LoadFrom($clientDll)
14: [Reflection.Assembly]::LoadFrom($versionControlClientDll)
15: [Reflection.Assembly]::LoadFrom($versionControlCommonDll)
16:
17: #Set up connection to TFS Server and get version control
18: $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsServer)
19: $versionControlType = [Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer]
20: $versionControlServer = $tfs.GetService($versionControlType)
21:
22: #Create a "workspace" and map a local folder to a TFS location
23: $workspace = $versionControlServer.CreateWorkspace("PowerShell Workspace",$versionControlServer.AuthenticatedUser)
24: $workingfolder = New-Object Microsoft.TeamFoundation.VersionControl.Client.WorkingFolder($tfsLocation,$localFolder)
25: $workspace.CreateMapping($workingFolder)
26: $filePath = $localFolder + "\" + $file
27:
28: #Submit file as a Pending Change and submit the change
29: $workspace.PendAdd($filePath)
30: $pendingChanges = $workspace.GetPendingChanges()
31: $workspace.CheckIn($pendingChanges,$checkInComments)
32:
33: #Delete the temp workspace
34: $workspace.Delete()
There’s probably a cool way to programmatically figure out where the dll’s that need to be registered are, but I leave that as an exercise to the reader
Entries (RSS)