How to automatically update TFS workitems from the command line

A lot of my life is lived according to bugs filed on a Team Foundation Server somewhere.  One of my particular projects of late has been requiring weekly updates to a number of TFS workitems.  I guess they want to make sure we’re still paying attention to them.

I set out to find a solution where I could automatically bulk update my work items at the click of the button.  Stretch goal: I wanted to have this happen automatically without any interaction.

Step 1: download TFS Powertools from: (make sure you get the version which matches your copy of Visual Studio

Command line tool documentation is located at: C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\Help\TFPTCommandLineTool.mht

NOTE: I will write this assuming you are using VS 2012.  Assume the path to other versions will be different.

Step 2: Make sure your connection to TFS is configured.

cd "C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools"
.\TFPT.exe connections

Verify that the correct TFS server is listed.

If the correct version of TFS is not configured:

  1. Open Visual Studio (2012)
  2. Click Team > Connect To Team Foundation Server
  3. Click Servers
  4. Click Add
  5. Configure the server

Step 3:

Create a query for the items you want to update.

The cron script will automatically update anything from a particular query.  You could just as easy specify work item IDs on the command line instead of using a query.  I decided against that because I wanted the flexibility to change the work items which get edited without having to change my code.

  1. Navigate to your TFS server in the browser (you could do this from Visual Studio Too)
  2. Click New > Query
  3. Build the query you want
  4. Save the query and make note of the location.
    In this case: Shiproom/My Queries/WeeklyAutoUpdate

NOTE: this query should actually say Changed Date < @Today – 6.  Not =.

Note that the last line is searching for @Today – 6.  I did that so that any work item I’ve updated within the last week isn’t touched by this query.

Step 4:

Verify that you are getting the correct work items IDs on the command line:  (replace http://tfsserver:8080/tfs with the server’s URL and the query name with your full query path)

PS C:\...> .\TFPT.exe query /collection:http://tfsserver:
8080/tfs /format:id "Shiproom/My Queries/WeeklyAutoUpdate"
942
945
946
947
948
PS C:\...>

Step 5:

Try editing an item through the command line.

Replace 942 with one of your work item IDs and once again replace http://tfsserver:8080/tfs with your server’s URL.

PS ...> .\TFPT.exe workitem /collection:http://tfsserver:8080/tfs /update 942 /fields:"History=."
Work item 942 updated.

Check to make sure your work item was updated.  It should now show a new entry in the history which merely contains a period.

Step 6:

Put it all together into a nice PowerShell script

$SERVER = "http://server:port/path" #Full URL to the server
$QUERY = "PATH/My Queries/QueryName" #Query of items to update
$UPDATECMD = '/fields:"History=."' #Command you want to run against them
$TFSPTPATH = "C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools"

Set-Location $TFSPTPATH

$idlist = .\TFPT.exe query /collection:$SERVER /format:id $QUERY

#could avoid the loop and send all IDs at once
foreach($id in $idlist)
{
    $result = .\TFPT.exe workitem /collection:$SERVER /update $id $UPDATECMD
    Write-Host $result
}

pause  #remove this to run without an end prompt

I chose to nest the work item update in a for loop.  This isn’t necessary.  TFPT will take a list of work item IDs and perform the same operation on all of them.  I general feel safer when I wrap something like that in my own loop.  That way one failure doesn’t necessarily kill everything.  Additionally, you get instant progress if there are a large number of workitems which must be updated.

The program as it is above has a call to pause on the last line.  If you want to run this as a scheduled task, that line should be commented out.

Step 7:

Add this to a scheduled task so that you aren’t bothered to remember your bugs.

Winning!

Thanks to:

This guy: http://ravendra.wordpress.com/2010/06/02/bulk-edit-tfs-work-items/ for giving me a lot of the concept.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s