Check a file into Team Foundation Server with PowerShell

We 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"
  12: #Load the Assemblies
  13: [Reflection.Assembly]::LoadFrom($clientDll)
  14: [Reflection.Assembly]::LoadFrom($versionControlClientDll)
  15: [Reflection.Assembly]::LoadFrom($versionControlCommonDll)
  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)
  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
  28: #Submit file as a Pending Change and submit the change
  29: $workspace.PendAdd($filePath)
  30: $pendingChanges = $workspace.GetPendingChanges()
  31: $workspace.CheckIn($pendingChanges,$checkInComments)
  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 🙂


4 Responses to “Check a file into Team Foundation Server with PowerShell”

  1. Robin Says:

    Even though your goal is just to lose 5 pounds in a week,
    you should be strict with what you do so as not to inadvertently undo
    all your hard work for vegetarian weight loss. Withdrawal from levothyroxine can be done but it takes 6 weeks of
    withdrawal for the remaining thyroid tissue to be
    completely starved. That way, the actual process will end up being much less frustrating and, for the majority of people, enjoyable.

  2. Minerva Says:

    Even though your goal is just to lose 5 pounds in a week, you
    should be strict with what you do so as not to inadvertently undo all your hard work for vegetarian weight loss.
    Authorities inside the area will need to have an intensive understanding of laboratory and labeling techniques, infection security precautions,
    appropriate blood attract approaches to the elderly and for infants and even more.
    Then leave it a while until your tummy lets you know
    that you’re hungry again before opting for a healthy snack.

  3. Says:

    a Bruce Lee workout includes stretching, bending, running,
    dipping, kicking, jumping, traditional muscle building exercises,
    weight lifting, rope skipping, medicine ball handling, etc.
    Anyone who dares to make a closer inspection will find the underlying difference.
    Hence, broccoli is a must-add ingredient in your vegetarian weight loss diet.

  4. Says:

    It does appear, however, that hypnosis can help a person achieve his
    or her weight loss goals. Avocados- Although not my favorite, are high
    in fats, the good ones. When you’re about to start on your weight loss plan, it’s a good idea
    to think about your end goal and chunk it down into smaller goals.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: