1

我想使用 Powershell 中的 REST API 向 TFS 项目添加标签。

我正在尝试根据 Visual Studio Integration的文档提出此请求

我这样称呼:

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Client')
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.WorkItemTracking.Client')

if ( (Get-PSSnapin -Name "Microsoft.TeamFoundation.Powershell" -ErrorAction                  SilentlyContinue) -eq $null )
{
Add-PSSnapin "Microsoft.TeamFoundation.Powershell"
}
$SrcCollectionUrl = 'http://tfs.myCompany.com:8080/tfs/MyCollection' 
$SrcProjectName = 'myProject' 
[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($SrcCollectionUrl)
[Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStoreFlags]$WorkItemBypass =      [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStoreFlags]::BypassRules

$tfstp = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($SrcCollectionUrl)
$WorkItemStore = New-Object -TypeName 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore' -ArgumentList $tfs.TfsTeamProjectCollection, $WorkItemBypass

$SrcProject = $WorkItemStore.Projects[$SrcProjectName]
$ProjectGuid = Split-Path $SrcProject.Uri -Leaf

$AddTagsUrl = '{0}/_apis/tagging/scopes/{1}/tags?api-version=1.0' -f $SrcCollectionUrl,$ProjectGuid

$newTagParams = @{name="PWCreateTag2"}
$outjson = $newTagParams | ConvertTo-Json
$nresp = Invoke-RestMethod -Method POST -Uri $AddTagsUrl -UseDefaultCredentials  -Body $outjson -ContentType 'application/json'

一切正常。第一次。但是文档指出:“如果该名称的标签已经存在,则不会创建任何标签。相反,响应正文包含具有该名称的现有标签。”

第二次调用我得到的行:“远程服务器返回错误:(400)错误请求。”

有人知道为什么第二次失败了吗?

仅供参考:TFS 服务器是 2015,Powershell 是 V4

4

1 回答 1

0

我为此创建了powershell模块 - tfs

添加标签:

'tag1','tag2' | % { Add-TFSBuildTag -Id 520 -Tag $_ }
于 2016-04-14T17:34:52.207 回答