Keeping your build agents clean

To keep our TFS Build Agents working smoothly, with as little maintenance as possible, we run a number of scheduled tasks each evening.

This script is run nightly

  • Clears out the local workspace left behind by every build definition
  • Removes the build folder and all the output's


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.VersionControl.Client")

$tfsUri = "http://_______:8080/tfs/defaultcollection";
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUri);

$vc = $tfsCollection.GetService([type] "Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer");

$localPC= $(hostname).ToUpper()

$workspaces = $vc.GetType().InvokeMember('QueryWorkspaces', 'InvokeMethod', $null, $vc, @($null, $null, $localPC))

foreach ($workspace in $workspaces)
{
    if ($workspace.Folders.length -gt 0)
    {
        $vc.GetType().InvokeMember('DeleteWorkspace', 'InvokeMethod', $null, $vc, @($workspace.name, "NTDomain\YourAccount"))
    }
}

Remove-Item "e:\builds" -recurse -force

Comments