PowerShell & TFS

There have been occasions where I've wanted to create work items in bulk.

For example: Every time you create a User Story your team has several standard Tasks that need to be created. Write Test Cases, Execute test cases, Deploy to QA, etc. I don't want to manually go through dozens or work items adding the same set of work items as children to each one over and over.

Excel is a pretty good option, certainly faster than doing it one at a time via Team Explorer or the Web interface. In the past I have written C# applications to do this through the TFS API. This works great and is very easy to code.

I am always trying to force myself to get better with PowerShell, so I started searching for examples of calling the TFS API from PowerShell. This helped me piece together the parts I needed to solve this problem with PowerShell.

These were my requirements:

  • There are N Stories in TFS each one represents an existing report that needs to go through several stages of work.
  • We want 6 new Stories created as children of each Report Story so the work can be assigned to different teams in the organization then the individual teams can create their own tasks.
  • We don't want the Child Stories to all have the same title. That is fine when you see them in context of their parent like this:
    • Report 1
      • Report Attributes
      • Data Lineage
      • Gap Analysis
    • Report 2
      • Report Attributes
      • Data Lineage
      • Gap Analysis
  • However when you see the Report Attributes Story on its own it means very little.
  • Therefore I want was something like this:
    • Report 1
      • Report Attributes for Report 1
      • Data Lineage for Report 1
      • Gap Analysis for Report 1
    • Report 2
      • Report Attributes for Report 2
      • Data Lineage for Report 2
      • Gap Analysis for Report 2
  • Plus each sub story needs to have the same Area, Team and other attributes as its Parent Story

As you can see a simple cut and paste in Excel won't do, because I would still have to edit each work item to add the report name to the title and populate the fields from the parent that I want brought over.

PowerShell to the rescue

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

 

#Get the TFS Collection

$tfsCollectionUrl = "http://Server:8080/tfs/CollectionName"

$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)

 

#Get the WorkItemStore Service

$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")

 

#Get the Team Project

$proj = $ws.Projects["Team Project"]

 

Write-Host "Team Project Collection: "$teamProjectCollection

Write-Host "Project:" $proj.Name

Write-Host "---------------------"

 

#Write a query to get the parent work items

$ParentWorkItems = $ws.Query("SELECT [System.Id] FROM WorkItems WHERE [System.AssignedTo] = 'Dave Lloyd' ")

 

#the Sub Stories you want to create as Children

$ChildStoryTitles = @( "Report Attributes","Data Lineage","Gap Analysis","Impact Analysis","Remediation","Testing")

 

#For each Parent Work item

foreach ($WorkItemParent in $ParentWorkItems)

{

$counter=1

Write-Host "Parent workItem:" $WorkItemParent.ID "-" $WorkItemParent.Title

 

#For each Parent create 6 New User Stories

foreach ($childTitle in $ChildStoryTitles)

{

$story = $proj.WorkItemTypes["User Story"]

$workitemChild = $story.NewWorkItem()

#Append the Parent Title to the end of the Child Title

     #System fields like title can be referenced directly and show up in intelliSence

$workItemChild.Title = $childTitle + " for " + $WorkItemParent.Title

#There is an order so I used a counter to set the Stack Rank on the CHild Stories

#For non-system fields use the fields member the name of the field and the value property

$workItemChild.Fields["Stack Rank"].value = $counter

#take any fields from the parent that you want to bring down into the child

$workItemChild.AreaId = $WorkItemParent.AreaId

$workItemChild.Save()

 

Write-Host " Child workItem" $workitemChild.ID $WorkItemChild.Title

 

#Link the CHild work item to the Parent Work item

$linkType = $ws.WorkItemLinkTypes[[Microsoft.TeamFoundation.WorkItemTracking.Client.CoreLinkTypeReferenceNames]::Hierarchy]

$link = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemLink($linkType.ReverseEnd, $workitemParent.ID)

$workitemChild.Links.Add($link)

$workItemChild.Save()

$counter++

}

 

}

 

//Build/Toronto

Couldn't go to //Build/? Not to worry Build is coming to you.

June 12th at the Design Exchange you can see Joe Stegma, Jeff Burtoft, Nikola Metulev and Vlad Kolesnikov deliver a day of sessions from Build.

Agenda

8:00 - close

Registration

8:00 - 9:00

Breakfast

9:00 - 10:30

Keynote

10:30 - 10:45

Break

10:45 - 11:45

Session #1 Universal Windows Platform

11:45 - 12:45

Lunch

12:45 - 13:00

Challenge

13:00 - 14:00

Session #2 Microsoft Edge & Web Apps

14:00 - 15:00

Session #3 Lightning Talks

15:00 - 15:30

Break

15:30 - 16:45

Panel Q/A

16:45 - 17:00

Closing remarks

 

Register here or if you can't get there but would like to join the Live Webcast register here.

ObjectSharp at the Movies!

If you haven't registered what are you waiting for?

Click the movie poster below to Register.

MVP Virtual Conference May 14th and 15th

World-class free online conference that features technical content presented by Americas' region MVPs that is open to the public

More technical content (Level 200, 300, 400), less marketing

5 tracks: IT Pro English, Dev English, Consumer English, Mixed Spanish, Mixed Portuguese

Event will be broadcast via Lync using L+ which enhances the conferencing capabilities of Lync

Two full days of sessions with simultaneous webcasts running across all 5 tracks

Thursday May 14th and Friday, May 15th

Start at 8am PT and running until 6pm PT (Pacific)

Day 1: 45 sessions + Keynote, Day 2: 50 sessions

Keynote on Day 1 to be delivered by Steve 'Guggs' Guggenheimer, Corporate VP of DX

On Demand content available via Channel9

This event is not just for MVPs, it's for everyone!

Great offers from Microsoft

Here are a couple of offers from Microsoft you may want to take advantage of.

Enjoy the free stuff, and you're welcome.

2015 ObjectSharp at the Movies

It's that time of year again. Time for "ObjectSharp at the Movies".

This year's line-up:

  • Shane Castle: Azure - Using the cloud for Dev/Test – Now!
  • Dave Lloyd & Max Yermakhanov: TFS 2015 - What Developers need to know about what's new.
  • Lori Lalonde & David Totzke: Build 2015 - All the key high lights from our O# Developers who were there.

     

    Click here to Register

Upcoming Lunch & Learns

Azure Websites - More than hosting

TL;DR Learn how to save your business money with websites and backups.

Description

In this session you'll learn the many different ways to deploy, manage & scale .NET, PHP, Node.js, Python and Java websites and web apps on Microsoft Azure. This includes cloud best practices like integrated source control & continuous deployment with Git & GitHub, auto scaling, web jobs, caching, backups, staging environments and more. Join Microsoft in this demo heavy overview of Microsoft Azure Websites and learn how eligible startups can get up to $60,000 in free Azure credits through BizSpark.

What you'll walk away knowing

Dates & Locations

Toronto - March 18

Vancouver - March 23

Kitchener - March 26

Montreal - March 26

Events Summary

Azure Websites - More than hosting

Toronto - March 18

Vancouver - March 23

Kitchener - March 26

Montreal - March 26

Azure IaaS for IT Pros Online Event (Dec 1-4)

Is it time to level up your skills using Infrastructure as a Service? Want to learn from the best technical engineers and noted technology leaders around?

Join Mark Russinovich, Corey Sanders, and members of the Azure Engineering team for a live event delivered online over four days. Join for one session, join for them all. You'll dive deep into technical scenarios, ask questions of the experts, and level up your own skills.

Azure IaaS for IT Pros Online Event, December 1-4
Click here to register!


Become an IaaS go-to guru
Mark Russinovich, Microsoft Chief Technology Officer, Azure, will kick off the event, followed by deep technical training in the technologies critical for IT Pro Implementers, like you, to deepen your foundational cloud skills. Following the keynote, experts will share their technical insights on topics including Core IaaS, Cloud Infrastructure Fabric, Open Source Software on Azure, and Workloads on Azure IaaS.

Be recognized for your skills
Get certified for your skills! Register for the event to receive reminder emails and obtain details for receiving a 50% off exam voucher you can use towards Exam 70-533: Implementing Microsoft Azure Infrastructure Solutions for Microsoft Azure Specialist Certification.

Join the live event from 9am to 1pm PST

December 1
Establish the Foundation: Core IaaS Infrastructure Technical Fundamentals

December 2
Dive Deep into Networking, Storage, and Disaster Recovery Scenarios

December 3
Embrace Open Source Technologies (Chef and Puppet Configurations, Containerization with Docker and Linux) to Accelerate and Scale Solutions

December 4
Optimize Windows Workload Architecture and Administration Capabilities Within Azure

Register Now

Replace a string in a file with PowerShell

I have wanted to expand my PowerShell skills for some time, however I find it a steep learning curve. One thing I am learning is the result is worth it.

I recently needed to replace a string in a file. I searched a found the working parts I needed create this. I just wanted to post it here so I know where to come back and find it.

 

[CmdletBinding()]

param

(

    [Parameter(Mandatory=$True)]

    [string]$OldValue,

    [Parameter(Mandatory=$True)]

    [string]$NewValue,

    [Parameter(Mandatory=$True)]

    [string]$FilePath

)

(Get-Content $FilePath) |

Foreach-Object {$_ -replace "$OldValue","$NewValue"|

set-content $FilePath

 

A wonderful addition to this is you can even pop regular expressions into the $OldValue.

 

(Get-Content MySSAS.deploymenttargets) |

Foreach-Object {$_ -replace "<Server>(.*?)</Server>","<Server>__TargetDataBase__</Server>"}  |

set-content MySSAS.deploymenttargets

 

 

 

From Requirements to Deployment: The Modern SW Developer using TFS

On May 8th, the Scotia Bank Theatre, Richmond Street Toronto was the scene of another successful “ObjectSharp At the Movies” event. The event was recorded so you can watch again, share with colleagues or maybe you couldn’t make the presentation and just want to watch for the first time. Below is a quick synopsis of what I presented. Click here to enjoy the Video.

My goal for the morning was simple. As MC I needed to keep things moving, entertain the audience during lull's and show the 500+ registered attendees how great it will be when they upgrade and or start using TFS 2013 in under 30 minutes.

I Started things off with this slide showing Team explorer when TFS is your repository and when Git is your repository. Not much to say on the subject as Colin covered it in his presentation.

image

The new Build process in 2013 includes the ability to shell out to a batch file or powershell script from several steps including prebuild, postbuild, pretest and posttest. This is a great feature that I have been adding to build processes since 2010.

A lot of people didn’t like the fact that pending changes was incorporated into the Team Explorer. Several of the Team explore windows like pending changes and builds can be torn off the team explorer and float as their own windows.

My Work is not new to 2013 but worth mentioning. This window is a great view of all your Work In Progress, Suspended Work, Available Workitems and Code Reviews. Watch the video to see how you can easily switch context using Suspended work and change the query behind Available Workitems to show the work that is assigned to you.

Check out Code Lens inserted right in your code on classes and methods, showing you references to this code, changesets when this code was added and workitems associated to those change sets. This is a great connection from your code directly into TFS.

Release Management is the biggest and best new feature of TFS 2013. Watch and see how a code change can easily be deployed out to multiple environments all starting from your build.

Again click here to enjoy the Video.