PowerShell+update TFS build properties

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…..

cleanup old logs, 1 line script in PowerShell

I was seeking for simple ways to cleanup old logs… google does pull out a lot of stuff, there were plenty of WMI/VBS/.cmd scripts…. however, they were so huge too… I wasn’t really convinced to go for 25+ lines of script just to clean up some log files on regular basis… all I wanted a simple, few lines (not more than 5 lines) of script…..
PowerShell is so sweet… here we go… 3 lines…

$a = get-Date
$a = $a.addDays(-15)
dir <path> | Where-Object { $_.LastWriteTime -lt $a } | remove-item -recurse -force

Upon scheduling this script, it’ll retain only last 15 days of file….delete all old (golden) junks…. 😉

I just enhanced it with a foreach to iterate list of files & delete them…. enjoy guys… PowerShell  – all good and powerful stuff…. nice job PowerShell team… this really makes windows simple …

TFS & PowerShell

PowerShell has got excellent features could be useful for SCM tasks….for example, I wanted to monitor history of set of files(say 25) on a weekly basis… I know history command will do but still did not want to do that manual work weekly… if we go for .cmd file… getting the date difference was a challenge… meaning…. everyweek we need to take the history for the past 7 days…which means you need to manipulate date variable… I wasn’t really sure whether its possible in batch scripts…

but powershell does that in 1 line…

executing tf.exe was simple enough…..

here we go….

$TFExecutablePath = “C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\..\IDE\TF.exe”
$file = <path>
$outputFile = <path>

$a=get-date
$a=$a.addDays(-7)
& $TFExecutablePath history $file /version:D$a~T | Out-File -filepath $outputFile -append

So sweet….. though scheduling this script took me a few hours, its great….. enjoy guys…. 🙂