Geeks With Blogs

The Life and Times of a Dev Yes, we're really that weird

Using scrum, there’s often tasks that you add to every story.  Things like “Unit Tests” and “Code Review”, etc.

Entering these in TFS is a pain, so I wrote a quick powershell script to do it for me.  This also contains an example of a function inside of a powershell script.  If you’re a c# guy, it’s important to note:  No parenthesis when calling the function, and no commas between parameters, use spaces instead.

You call it by calling the script name and passing in as a parameter the story number that you want to add the tasks to.

Enjoy!

 

# You'll need the powertools for TFS.  2015 version is at https://marketplace.visualstudio.com/items?itemName=TFSPowerToolsTeam.MicrosoftVisualStudioTeamFoundationServer2015Power
#############################################################
#
# Description: Automatically creates standard Tasks
#              in TFS for a given story.
#
# Created:     2/7/2017
#
#############################################################
Param(
    [string]$storyNumber
)

# Clear Output Pane
#clear

# setup some constants
[string] $tfsServerUrl = http://[YourServer]/tfs/[YourCollection]
[string] $projectName = "[YourProject]";

# Loads Windows PowerShell snap-in and related assemblies if not already loaded
if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
    Add-PSSnapin Microsoft.TeamFoundation.PowerShell
}

[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")

function AddTask($workItemStore, $taskType, $story, $title, $activity, $hours)
{
    $task = New-Object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem -ArgumentList $taskType
    $task.Title = $title
    $task.AreaId = $story.AreaId
    $task.IterationId = $story.IterationId
    $task["Activity"] = $activity
    $task["Remaining Work"] = $hours
    $task.Save()

    $linkType = $workItemStore.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]
    $link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $story.ID)
    $result = $task.Links.Add($link)
    $task.Save()
    Write-Host $title " task created."
}

# get the TFS collection
$collection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsServerUrl)

# get the workitem store
$workitemStore = $collection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")

# get the team project
$project = $workitemStore.Projects[$projectName]

# get the story
$story = $workItemStore.GetWorkItem($storyNumber)

# get the task task type
[Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType]$taskType = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType]$project.WorkItemTypes["Task"]

# Add the tasks.
Write-Host "Adding boilerplate to Story " $story.Id " - " $story.Title

AddTask $workitemStore $taskType $story "Your Title" "Your Activity" YourHours

Posted on Tuesday, February 7, 2017 5:57 PM | Back to top


Comments on this post: TFS script to automatically add tasks

# re: TFS script to automatically add tasks
Requesting Gravatar...
These ideas are very useful in the project that I am working on. - Steven C Wyer
Left by William Sparow on Feb 08, 2017 1:14 PM

# re: TFS script to automatically add tasks
Requesting Gravatar...
Thanks for sharing this post. I will make a note.
http://www.webrifer.com/seo.html
Left by Web Rifer Techonologies on Feb 08, 2017 9:29 PM

# re: TFS script to automatically add tasks
Requesting Gravatar...
That is really a helpful post. It help many othres as well.
http://www.advocosoft.com/
website development company in jaipur
Left by Ashley on Feb 09, 2017 3:21 AM

# re: TFS script to automatically add tasks
Requesting Gravatar...
Great work. Thanks a lot for sharing it with us.
PlayBox for iOS
Left by Darren Powell on Feb 23, 2017 5:47 AM

Your comment:
 (will show your gravatar)


Copyright © Robert May | Powered by: GeeksWithBlogs.net