function set-RetainIndefinitely { [CmdletBinding()] param( [bool] $retainIndefinitely, # Defaults to false [string] $tfsUrl = $(Throw 'TFS Url is required'), [string] $teamProject = $(Throw 'Team Project is required'), [string] $buildDefinition = $(Throw 'Build Definitioin is required'), [string] $buildNumber = $(Throw 'Build Number is required') ) # The machine you use to execute this PowerShell must have Team Explorer installed # so that we can load the needed assemblies. # The [void] stops the GAC messages from being output to screen. Write-Verbose "Loading assemblies" [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") # Connect to TFS Write-Verbose "Connecting to TFS" $teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUrl) # Get the build service $buildServer = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Build.Client.IBuildServer") # Find the build used for this release $buildSpec = $buildServer.CreateBuildDetailSpec($teamProject, $buildDefinition) # Don't query any additional information i.e. associated work items, associated changesets, etc. $buildSpec.InformationTypes = $null # Use the build number to find the build $buildSpec.BuildNumber = $buildNumber # Search for the build $results = $buildServer.QueryBuilds($buildSpec) # Make sure we only found a single build if($results.Builds.Count -eq 1) { Write-Verbose "Build Found" $build = $results.Builds[0] $build.KeepForever = $retainIndefinitely $build.Save() Write-Verbose "Retain Indefinitely flag set to $retainIndefinitely" } }