尝试通过PowerShell创建新工作项时,项目集合获取的服务不返回任何值

时间:2016-07-16 20:54:54

标签: powershell tfs tfs2015

我正在尝试使用powershell和tfs api创建一个新的工作项类型。

这是我尝试运行以创建新工作项类型

的脚本
[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

当我运行此脚本时,收到一条错误,指出无法索引到空数组。在$ proj = $ ws.Projects [" TestTeamProject"]

我的猜测是workitemstore变量$ ws为null,我错过了该行中的任何内容吗?

1 个答案:

答案 0 :(得分:0)

[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

将$ ws类型更改为Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore类型并取消了GetService,现在工作正常...