A cacophony of ramblings from my potpourri of notes
 Tuesday, September 26, 2006
Creating a TFS Work Item from PowerShell

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)
>$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).

>$workItemStore = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]
>$store=$tfs.GetService($workItemStore)
>$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}
Attachments : {}
Links : {}
Store : Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore
Title : The application isn't working
State : Proposed
Reason : New
Rev : 1
Description :
History :
ChangedBy : Mark Michaelis (Administrator)
RevisedDate : 1/1/0001 12:00:00 AM
ChangedDate : 9/26/2006 2:49:10 AM
CreatedDate : 9/26/2006 2:49:10 AM
CreatedBy : Mark Michaelis (Administrator)
NodeName : MSF for CMMI
AreaPath : MSF for CMMI
AreaId : 153
IterationPath : MSF for CMMI
IterationId : 153
ExternalLinkCount : 0
HyperLinkCount : 0
AttachedFileCount : 0
RelatedLinkCount : 0
IsOpen : True
IsPartialOpen : False
IsReadOnlyOpen : False
Fields : {Title, State, Rev, Changed By...}
Type : Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType
Project : Microsoft.TeamFoundation.WorkItemTracking.Client.Project
IsDirty : False DisplayForm :
DisplayForm : <FORM>
                  <...>
              </FORM>                                                                   

Just to make sure, I run a query for all active bugs to verify that mine is there.

>$store.Projects["MSF for CMMI"].StoredQueries | ft Name
Active Bugs
All My Team Project Work Items
All Tasks
All Work Items
Blocked Work Items
Change Requests
Copy of Issues
Corrective Actions
Customer Requirements
Development Tasks
Issues
Mitigation Action Status
My Work Items
Product Requirements
Resolved Bugs
Reviews
Risks
>$query=$store.Projects["MSF for CMMI"].StoredQueries | where{$_.Name -eq "Active Bugs"}
>$store.Query($query.QueryText.Replace("@project", "'MSF for CMMI'"))
...

One PowerShell question I couldn't find the answer on, is how do I enter a password without displaying it on the screen.  The way RunAs works for example.


Tuesday, September 26, 2006 2:36:19 AM (Pacific Standard Time, UTC-08:00)  #    Comments [5]  Computer Related | .Net | Headlines