Let's say you have an action you want to perform against nearly every collection on your TFS instance. For example, maybe you just fixed an issue with your mail server and want to force the "Team Foundation Server Event Processing" job for each collection to run soon. (By default it runs every 30 minutes if there are no client actions like checkins triggering it.)
Below are Powershell examples you can customize. When you are operating on several collections you should be prepared for some to be offline, unless you know otherwise. I use trap below to handle this expected condition.
Which example do you want to use? If you're an instance-level TFS administrator and you're performing an administrative action, use the ITeamProjectCollectionService. Otherwise, use the catalog. Taylor Lafrinere covers this in more detail in one of his blog posts. I'm mainly converting his C# examples into Powershell examples here.
Why "nearly" every collection? See my caveats below.
Using the ITeamProjectCollectionService.
Three caveats:
Notes:
Code:
[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
#Update configuration URL as necessary
$configServer = new-object Microsoft.TeamFoundation.Client.TfsConfigurationServer "http://localhost:8080/tfs/"
$collectionService = $configServer.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService])
$collectionService.GetCollections() | %{
$collectionName = $_.Name
$collectionId = $_.Id
trap [Microsoft.TeamFoundation.TeamFoundationServiceUnavailableException]
{
write-warning ("Skipping project collection "+ $collectionName +" ("+ $collectionId +"): "+ $_.Exception.Message)
continue
}
&{
$projectCollection = $configServer.GetTeamProjectCollection($collectionId)
# Check that we can access the team project collection.
$projectCollection.Connect("None")
write-host
write-host ("* Operating on project collection "+ $collectionName +" ("+ $collectionId +").")
# Now do your per-collection action.
# For example, queue the "Job History Cleanup Job" for this collection
# $jobService = $projectCollection.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamFoundationJobService])
# $jobService.QueueDelayedJob('8fc76967-da5d-4d53-937b-e3efc6af0fcb')
# Your code here.
Using the TFS Catalog:
$configServerNode = $configServer.CatalogNode;
# Query the children of the configuration server node for all of the team project collection nodes
$tpcNodes = $configServerNode.QueryChildren(
[Guid[]] @( [Microsoft.TeamFoundation.Framework.Common.CatalogResourceTypes]::ProjectCollection ),
$false,
"None");
$tpcNodes | %{ $_.Resource } | %{
$collectionName = $_.DisplayName
$collectionId = $_.Properties["InstanceId"]