Skip to content

Instantly share code, notes, and snippets.

@deadlydog
Last active May 13, 2016 16:35
Show Gist options
  • Save deadlydog/b70493cc9c22505af534 to your computer and use it in GitHub Desktop.
Save deadlydog/b70493cc9c22505af534 to your computer and use it in GitHub Desktop.
In TFS 2012 a "bug" was introduced that transformed the functionality of the Priority and StackRank fields so that they get updated to a large random number when a work item is reordered in the web access UI, effectively wiping out the previous value that had been set manually (and purposely) by a human being. This script reverts the Priority an…
Set-StrictMode -Version Latest
$TFS_TEAM_PROJECT_COLLECTION_URI = 'YourProjectCollectionUrl' # e.g. https://tfs.YourDomain.com/tfs/ProjectCollection
Function RevertField ($WorkItemCollection, $FieldName)
{
$WorkItemCount = $WorkItemCollection.Count
$WorkItemCounter = 0
Foreach ($WorkItem in $WorkItemCollection) {
Write-Progress -Activity "Reverting Field '${FieldName}': ${WorkItemCounter} of ${WorkItemCount}" -Status $WorkItem.Fields['Title'].Value -PercentComplete ($WorkItemCounter/$WorkItemCount*100)
$WorkItemCounter++
#Write-Host $WorkItem.Fields['Title'].OriginalValue -ForegroundColor Gray
$Revisions = $WorkItem.Revisions
# Open Work Item
$WorkItem.Open()
$FieldValue = $WorkItem.Fields[$FieldName].Value
# If the revisions are not null and there is at least on revision
If ($Revisions -ne $null -and $WorkItem.Revisions -ne 0)
{
# Iterate over all revisions
Foreach ($Revision in $Revisions) {
# We need the revision where the original value was under 1000 and the new value was over 1000
If ($Revision.Fields[$FieldName].Value -gt 1000 -and $Revision.Fields[$FieldName].OriginalValue -lt 1000)
{
$FieldValue = $Revision.Fields[$FieldName].OriginalValue
}
}
}
# Add new value and save
$WorkItem.Fields[$FieldName].Value = $FieldValue
$WorkItem.Save()
$WorkItem.Close()
}
}
# Load the required assemblies
$MicrosoftTeamFoundationClient = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
$MicrosoftTeamFoundationVersionControlClient = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.WorkItemTracking.Client")
# Get team project collection
$TfsTeamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection([Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]::GetFullyQualifiedUriForName($TFS_TEAM_PROJECT_COLLECTION_URI))
# Get work item store
$WorkItemStore = $TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
# Get a list of work items with inflated Priority
$QueryString = "SELECT * FROM WorkItems Where [Microsoft.VSTS.Common.Priority] > 4"
$WorkItemCollection = $WorkItemStore.Query($QueryString)
# Revert all Priority Fields
RevertField $WorkItemCollection 'Priority'
# Get a list of work items with inflated StackRanks
$QueryString = "SELECT * FROM WorkItems Where [Microsoft.VSTS.Common.StackRank] > 4"
$WorkItemCollection = $WorkItemStore.Query($QueryString)
# Revert all StackRank Fields
RevertField $WorkItemCollection 'Stack Rank'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment