简体   繁体   中英

Project collection get service returning no value when trying to create a new work item via PowerShell

i was trying to create a new work item type using powershell and tfs api.

here is the script which i am trying to run to create a new work item type

[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") 
[string] $tfsCollectionUrl = "http://r2-09-tfs:8080/tfs/DefaultCollection"
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
$ws = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore")
$proj = $ws.Projects["TestTeamProject"]
$wit = $proj.WorkItemTypes["Task"]
$workitem = $wit.NewWorkItem()
$workItem.Title = "Sample Task Title 2"
$workItem.Description = "Sample Description"
$workitem.AreaPath = "TestTeamProject"
$workitem.IterationPath = "TestTeamProject"
$workItem.Save()
Write-Host "The TFS work item number is: " $workItem.Id

when i run this script, getting an error which says Cannot index into a null array. At $proj = $ws.Projects["TestTeamProject"]

my guess is that the workitemstore variable $ws is null, am i missing anything in that line?

[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") 
[string] $tfsCollectionUrl = "http://r2-09-tfs:8080/tfs/DefaultCollection"
$teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl)
[Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]$ws = $teamProjectCollection
$proj = $ws.Projects["TestTeamProject"]
$wit = $proj.WorkItemTypes["Task"]
$workitem = $wit.NewWorkItem()
$workItem.Title = "Sample Task Title 2"
$workItem.Description = "Sample Description"
$workitem.AreaPath = "TestTeamProject"
$workitem.IterationPath = "TestTeamProject"
$workItem.Save()
Write-Host "The TFS work item number is: " $workItem.Id

changed $ws type to Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore type and took off GetService, works fine now...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM