Friday, April 26, 2013

TFS 2012 how to get Last Known good build via Powershell

The following script will return the last successful build on a given build definition. 

Save the following to a .ps1 file

$project = "testproject"
[URI]$Projecturi = "http://tfsserver:8080/tfs/DefaultCollection/"
function Wait-GetLKGBuild {


 $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($Projecturi)
    $buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
    $rbBuildDefinition = $buildserver.GetBuildDefinition($project, $buildDefinitionName)
 $build = $buildserver.GetBuild([Uri]($rbBuildDefinition.LastGoodBuildUri));
  New-Object PSObject -Property @{
                Buildnumber = $build.BuildNumber; 
    Requestedfor = $build.RequestedFor;
                Status = $build.Status;           
                CompilationStatus = $build.CompilationStatus;           
                Start = $build.StartTime;           
                End = $build.FinishTime;           
                DropLocation = $build.DropLocation;           
                SourceGetVersion = $build.SourceGetVersion;           
Wait-GetLKGBuild $buildDefinitionName


Unknown said...

Thanks. Great post. Can know the build Agent number also?

Anonymous said...

full detailled article and powershell kit to query tfs build using powershell can be found there :

Mr Roaster said...


Any way to additionally query the TestRunStatus? Would be the perfect report:

CompilationStatus: Succeeded
TestRunStatus: Succeeded

freakydinde said...

sure,try this :

Mr Roaster said...

Thanks freaky :)