I came across a PowerShell script for creating TFS Work Items from Alan Stevens today. There are a couple more thougts and ideas I would add. Here is the output from my interactive PowerShell session (notice that I start with Alan's code to load the TFS assemblies. However, I begin to vary shortly after that. >$key = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0 >$dir = [string] (Get-ItemProperty $key.InstallDir)$dir += "PrivateAssemblies\" >$lib = $dir + "Microsoft.TeamFoundation.WorkItemTracking.Client.dll"[Reflection.Assembly]::LoadFrom($lib)>$lib = $dir + "Microsoft.TeamFoundation.Client.dll"[Reflection.Assembly]::LoadFrom ($lib) >"Please enter your Team Foundation Server Name:"$server = [Console]::ReadLine()$server = $server.Trim() >$server="tfs" >$NetCredentials=new-object System.Net.NetworkCredential ("Mark", "P@ssw0rd", "Michaelis") >$tfs=new-object Microsoft.TeamFoundation.Client.TeamFoundationServer($server, $NetCredentials) > To begin with I instantiate the TeamFoundationServer object rather than calling GetService() . This is because I want to change the network credentials that I use to connect to the server (see Buck Hodges post on this ). >$type = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore] >$workItemStore = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore] $ tfs .GetService($type) >$bug=$store.Projects["MSF for CMMI"].WorkItemTypes["Bug"].NewWorkItem() >$bug.Title="The application isn't working" >$bug.Save() Exception calling "Save" with "0" argument(s): "TF26201: This work item has unsupported fields, or user does not have permissions." > The next problem was that saving the bug results in an error. I am pretty sure it is not permissions since I specified the credentials explicitly. This leaves the unsupported fields option (whatever that means). I check the Fields property and determine what the required fields are. After setting these fields I am able to store the bug. A non-zero ID and the revision number are just a few properties that verify the work item was successfully stored. >$bug.Fields | where {$_.IsRequired -and (-not $_.IsValid)} | ft Name Name ---- Symptom Steps To Reproduce >$bug.Fields["Symptom"].Value="Headache" >$bug.Fields["Steps To Reproduce"].Value="Drink too much alcohol" >$bud.Save() >$bug.Id 183 >$store.GetWorkItem($bug.Id) Id : 183 Uri : vstfs:///WorkItemTracking/WorkItem/183 Revision : 1 Revisions : {Microsoft.TeamFoundation.WorkItemTracking.Client.Revision}
Read More...