Powershell script to make use of @CurrentIteration query token in TFS queries
Since the introduction of Visual Studio 2013 update 5, a new query token (@CurrentIteration) is available to automatically identify the current iteration of your iteration path, based on today’s date. By using this query token in queries that are related to the current iteration/sprint, you will save a lot of time in managing those queries (as mentioned in my previous blogpost).
However, once upgraded to Visual Studio 2013 update 5 or higher, you have to manually update your queries to make use of this new @CurrentIteration query token. This may cost you a lot of time. For that reason, I’ve made a powershell script to automatically implement the @CurrentIteration query token for the shared queries of your team project (see the code below or the attached zip file). After downloading/unzipping, just specify your own values for the different variables and run the powershell script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
[crayon-5c6be37ea2e56063926811 ]# Load TFS PowerShell Snap-in if ((Get-PSSnapIn -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin Microsoft.TeamFoundation.PowerShell } # Load Referenced Assemblies [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common") [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Net.Primitives") # Variables $tfsCollectionUrl = "[TEAM PROJECT COLLECTION]" $tfsTeamProject = "[TEAM PROJECT]" $username = "[USERNAME]" $password = "[PASSWORD]" $currentIterationToReplace = "'[TEAM PROJECT][IterationX]'" # Connect with the TFS collection $tfsServer = New-Object System.Uri($tfsCollectionUrl) $netCred = New-Object System.Net.NetworkCredential($username,$password,"") $basicCred = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential($netCred) $tfsCred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials($basicCred) $tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$tfsCred) $tfsCollection.EnsureAuthenticated() if ($tfsCollection.HasAuthenticated) { #Get Work Item Store object $ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore") #Get work item store of Team Project $proj = $ws.Projects[$tfsTeamProject] # Update all queries of team project $queryHierarchy = $proj.QueryHierarchy; $queryRootFolder = $queryHierarchy -as [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryFolder] $AllQueryDefsOfProject = GetAllQueriesOfQueryFolder $queryRootFolder foreach ($item in $AllQueryDefsOfProject) { $queryDef = $item -as [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryDefinition] $newQueryText = $queryDef.QueryText.Replace($currentIterationToReplace, "@CurrentIteration") $queryDef.QueryText = $newQueryText } $proj.QueryHierarchy.Save() } function GetAllQueriesOfQueryFolder([Microsoft.TeamFoundation.WorkItemTracking.Client.QueryFolder] $queryFolder) { $AllQueryDefinitions = @() foreach ($item in $queryFolder) { $queryItem = $item -as [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryItem] if($queryItem -is [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryFolder]) { $subQueryFolder = $queryItem -as [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryFolder] $AllQueryDefinitions += GetAllQueriesOfQueryFolder $subQueryFolder; } elseif($queryItem -is [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryDefinition]) { $queryDef = $queryItem -as [Microsoft.TeamFoundation.WorkItemTracking.Client.QueryDefinition] $AllQueryDefinitions += $queryDef } } return $AllQueryDefinitions; } |
[/crayon]
EnableCurrentIterationQueryToken