使用Powershell中的TFS api获取特定项目的工作项

时间:2017-12-12 10:42:43

标签: rest tfs powershell-v3.0 tfs-workitem

您好我正在尝试使用以下代码获取特定项目的工作项,但我得到所有项目的工作项计数。请帮帮忙?

$user = "username"
$token = "PAT"
$count=0
Function QueryWorkItem{

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


$uri = "http://(url)/tfs/DefaultCollection/(project name)/_apis/wit/wiql?api-version=2.2"
$body = "{
'query':'Select [System.WorkItemType],[System.Title],[System.State],[System.Id] FROM WorkItems '
}"
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body

foreach($i in $result.WorkItems){
$result2 = Invoke-RestMethod -Uri https://(uri)/_apis/wit/workItems/$($i.id)?api-version=2.2 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get
foreach ($j in $result2.fields )
{

    if ($j.'System.WorkItemType' -cmatch "Feature")
{

Write-Output ("work item ID: $($i.id) ") |  Out-File -Append Tasks.txt
Write-Output "WorkItem type:" $j.'System.WorkItemType'|  Out-File -Append Tasks.txt
Write-Output "Title:" $j.'System.Title'|  Out-File -Append Tasks.txt
Write-Output "Team Project:" $j.'System.TeamProject'|  Out-File -Append Tasks.txt
Write-Output "--------------------------------------------------------"|  Out-File -Append Tasks.txt

$global:count++
}

}
}

    }

    QueryWorkItem
      Write-Output "count:" $count

3 个答案:

答案 0 :(得分:1)

PowerShell CmdLets提供了一系列功能,可以将客户端对象模型或直接调用到REST API中。

https://github.com/igoravl/tfscmdlets

示例:

# Connect to the FabrikamFiber team project collection
# (Will be used as default for the -Collection argument when required by a cmdlet)
Connect-TfsTeamProjectCollection http://vsalm:8080/tfs/FabrikamFiberCollection

# Get a list of team projects in the currently connected TPC
Get-TfsTeamProject

# List the existing iterations in the FabrikamFiber team project
Get-TfsIteration -Project FabrikamFiber

# Connect to the FabrikamFiber team project
# (will be used as default for the -Project argument when required by a cmdlet)
Connect-TfsTeamProject FabrikamFiber

# Get all bugs in the current team project
Get-TfsWorkItem -Filter '[System.WorkItemType] = "Bug"'

答案 1 :(得分:1)

查询应如下所示:

Select [System.WorkItemType],[System.Title],[System.State],[System.Id] FROM WorkItems WHERE [System.TeamProject] = @project

更多信息,请参阅以下链接:

https://docs.microsoft.com/en-us/vsts/collaborate/wiql-syntax

答案 2 :(得分:0)

您的工作项查询不会在System.TeamProject上进行过滤,因此它会在团队项目中进行查询。在查询中添加WHERE子句。