update-feature.ps1 – example of querying and updating work items


Did a little script this week that updates one work item (the "feature") based on summing values in other work items (the tasks associated with that feature).  It may be of limited value to others as-is, but it should serve as a useful example, as it leverages the get-tfs.ps1 script.  Hopefully it's sufficiently self-documenting. 🙂  Obviously, it's currently hard-coded for our dogfood TFS server, but you can add that as another param (or just change it to your server) if you really want to use this script as-is.


Features it leverages:



  • get-tfs's work to fetch the WorkItemStore so we don't have to do that here

  • the Item parameterized property of WorkItem's so we can just index into them based on the fields we want to check

  • PowerShell's here-string along with using -f (string.format) so we don't need to do any weird quoting or escaping in our query 🙂

 

param (
$featureId = $(throw 'featureId parameter is required')
)

$tfs = get-tfs http://tkbgitvstfat01:8080
$taskQuery = @'
select [Microsoft.VSTS.Scheduling.RemainingWork],[Microsoft.VSTS.Scheduling.CompletedWork]
from workitems
where [Microsoft.VSTS.Dogfood.FeatureID]="{0}"
and [system.reason] != "Obsolete"
and [system.workitemtype] = "task"
'@ -f $featureId

trap { "Cannot find feature id $featureId"; break; } $feature = $tfs.wit.GetWorkItem($featureId)
$currentRemainingWork = $feature['Microsoft.VSTS.Scheduling.RemainingWork']
$currentCompletedWork = $feature['Microsoft.VSTS.Scheduling.CompletedWork']

$tasks = @($tfs.wit.Query($taskQuery))

$remainingWorkTotal = $(
$tasks | %{ $_['Microsoft.VSTS.Scheduling.RemainingWork'] } | measure-object -sum
).sum

$completedWorkTotal = $(
$tasks | %{ $_['Microsoft.VSTS.Scheduling.CompletedWork'] } | measure-object -sum
).sum

write-host "Calculated remaining work: $remainingWorkTotal"
write-host "Calculated completed work: $completedWorkTotal"

if ($currentRemainingWork -ne $remainingWorkTotal -or
$currentCompletedWork -ne $completedWorkTotal)
{
write-host "Updating feature $featureId values (were remaining=$currentRemainingWork and completed=$currentCompletedWork)"
$feature['Microsoft.VSTS.Scheduling.RemainingWork'] = $remainingWorkTotal
$feature['Microsoft.VSTS.Scheduling.CompletedWork'] = $completedWorkTotal
$feature.Save()
}
else
{
write-host "Feature $featureId values were already up to date"
}

update-feature.ps1


Skip to main content