If you are wondering how to update some build properties like ‘build quality’ or overwrite ‘retention policy’ or some other TFS Build property dynamically/scriptically…. PowerShell is a good option….
After I deploy the build on a test environment, I wanted to update ‘build quality’ and ‘lock’ a specific build for reference – so that my retention policy doesn’t wipe off the build….. naturally Visual Studio Team Foundation Client provides a tiny ‘lock’ symbol and it does provide interface to manage build quality too…. they appear right on the build explorer window and are simply perfect….however, I tend to forget that operation post deploy – meaning the way I got my deploy is completely unattended……of course, my retention policy retains only 5 builds…its embarrassing when a developer seeks a specific build inorder to reproduce a defect….. 😦
When I was seeking ways to automate this task..certainly there are ways like MSBuild custom task etc…. but I hate to write c# code, compile, reference that in MSBuild …. PowerShell was an elegant and neat option for my need…. loads the assembly at runtime…. no compilation, msbuild etc….
So the following script can be used to set build properties automatically post deploy – simply call powershell.exe with appropriate parameters….
here we go..
#must pass inputs in the following order
#1. TFS Uri 2. project name 3. build definition 4.build number 5. build quality 6. retain(true,false)
param (
[string]$TfsServerUri = $args[0],
[string]$ProjectName = $args[1],
[string]$BuildDefinitionName = $args[2],
[string]$MyBuildNumber = $args[3],
[string]$MyQaluty = $args[4],
[bool] $retainBuild= $args[5]
)
$ErrorActionPreference = “Stop” ;
if ($Verbose) { $VerbosePreference = “Continue” ; }
Write-Verbose -Message “Loading TFS Build assembly…”
[void][System.Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Build.Client, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)
Write-Verbose -Message “Loading TFS PowerShell snapin…”
$TfsSnapin = Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue ;
if ($TfsSnapin -eq $null) {
Add-PSSnapin -Name Microsoft.TeamFoundation.PowerShell ;
}
Write-Verbose -Message “Getting TFS server instance…”
if ([string]::IsNullOrEmpty($TfsServerUri)) {
$TfsServer = Get-TfsServer -Path (Get-Location) ;
} else {
$TfsServer = Get-TfsServer -Name $TfsServerUri ;
}
Write-Verbose -Message “Querying builds…”
$BuildServer = $TfsServer.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]) ;
$DetailSpec = $BuildServer.CreateBuildDetailSpec($ProjectName, $BuildDefinitionName) ;
$DetailSpec.QueryOrder=”FinishTimeDescending”;
$DetailSpec.MaxBuildsPerDefinition=10;
$Builds = $BuildServer.QueryBuilds($DetailSpec).Builds;
$(foreach ($build in $Builds) {
if ($build.BuildNumber -match $myBuildNumber)
{
$build.Quality=$myQuality;
$build.KeepForever=$retainBuild;
$BuildServer.SaveBuilds($build);
}
});
hope this helps someone…..