In the Orcas release we allow you to Query for builds (and build definitions) through the object model. However, the number of things you can filter the query on is somewhat limited.

One of the things that I find that I need to query builds for is the Changeset that they are associated with. Here's the scenario: I checked in a fix sometime in the past that fixed a bug, but my test team says that the bug is still broken in the latest build. I would like to see which build included my fix. Well, it's easy enough for me to look at the bug in TFS and see which changeset is associated with the fix, but I can't easily see which build contained that changeset. So, I need to query the Build Server to get my answer. Unfortunately, the object model does not easily allow me to query on builds by the associated changeset numbers, so, I wrote a PowerShell script.

The basic idea is that we need to Query for all Builds for a team project, look at each one's associated changesets, and return a list of only those that are associated with the changeset number we care about.

Here's the code:

     $serverName = "http://server:8080"
     $teamproject = "projectname"
     $changesetId = 123

     $buildserver = Get-BuildServer($servername)

     $spec = $buildserver.CreateBuildDetailSpec()
     $spec.DefinitionPath = "\" + $teamproject + "\**"
     $spec.BuildNumber = "*"
     $spec.QueryOptions = "None"
     $spec.InformationTypes = "AssociatedChangeset"

     $queryResult = $buildserver.QueryBuilds($spec)

     $queryResult.Builds | foreach-object {
         $build = $_
         $associatedChangesetNodes = $build.Information.Nodes
         if ($associatedChangesetNodes)
         {
             $changesets = [int[]]@($associatedChangesetNodes | foreach-object { $_.Fields["ChangesetId"] })
             if ($changesets -contains $changesetId)
             {
                 add-member -in $build noteproperty "AssociatedChangesets" $changesets -passthru
             }
         }
     }

This script and other useful Build related PowerShell scripts are attached to my script post.

I called this script  Get-BuildsForChangeset.ps1 and allowed the server name, team project and changeset number to be passed in. Here is an example of the call and the output:

     > Get-BuildsForChangeset 'http://jpricket-test:8080' 'jpricket-032707-2' 6

     AssociatedChangesets : {6}
     BuildServer : Microsoft.TeamFoundation.Build.Client.BuildServer
     BuildAgent :
     BuildDefinition :
     BuildFinished : True
     BuildNumber : Nightly_20070329.2
     CompilationStatus : Succeeded
     ConfigurationFolderPath : $/jpricket-032707-2/TeamBuildTypes/Nightly
     DropLocation : \\jpricket-test\drops\Nightly_20070329.2
     FinishTime : 3/29/2007 1:32:15 PM
     Information : Microsoft.TeamFoundation.Build.Client.BuildInformation
     LogLocation : \\jpricket-test\drops\Nightly_20070329.2\BuildLog.txt
     Quality :
     Status : Succeeded
     TestStatus : Succeeded
     Uri : vstfs:///Build/Build/9
     BuildDefinitionUri : vstfs:///Build/Definition/1
     CommandLineArguments :
     StartTime : 3/29/2007 1:31:48 PM
     BuildAgentUri : vstfs:///Build/Agent/1
     ConfigurationFolderUri : vstfs:///VersionControl/VersionedItem...
     ConfigurationFolderGetTime : 3/29/2007 1:31:48 PM
     SourceGetVersion : C6
     RequestedFor : NORTHAMERICA\jpricket
     RequestedBy : NORTHAMERICA\jpricket
     LastChangedOn : 3/29/2007 1:32:14 PM
     LastChangedBy : REDMOND\bissvc
     KeepForever : False
     LabelName : Nightly_20070329.2@$/jpricket-032707-2