我试图使用powershell和json将父链接添加到TFS工作项。我们有一个内部的TFS服务器(即,不是团队服务)。
我得到了查询的答案,所以我与TFS的连接正常工作,但是当我试图更新时,我会得到以下错误:
"You must pass a valid patch document in the body of the request."我是一个json成员,从这个MSDN页面上得到了我的json骨架。
这是我的儿子:
[
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
"attributes":
{
{ "isLocked": false }
}
}
}
]基于我找到的其他一些json示例,我在几个地方用方括号进行了测试,但是它们没有帮助,所以我回到了上面的MSDN页面的语法。
这就是我正在使用的powershell脚本。
param(
[System.String]$TaskId=288346
)
$username = "myUserInfo"
$password = "myPassword"
$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)
$taskItemURL ="https://tfs.myCompanynet.org/tfs/DefaultCollection/_apis/wit/workitems/$TaskId"
$taskItemRequest = $taskItemUrl+'?$expand=relations'
$taskItemJson = Invoke-RestMethod -uri "$taskItemRequest" -Method get -
Credential $credential
if($taskItemJson.relations)
{
write-host "relation exists: " $taskItemJson.relations[0].url
}
else
{
write-host "relation does not exist. Creating it."
$jsonTemplate = Get-Content E:\scripts\JsonTemplate.txt # | ConvertTo-Json
$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -UseDefaultCredentials -ContentType application/json-patch+json -body $jsonTemplate
}如您所见,我已经注释掉了转换到- JSON,因为我收到了这个错误:ConvertTo:转换后的JSON字符串格式很差。我不确定我是否收到了那个错误,因为它已经是json了。
我还测试了跳过get内容和使用-inFile参数,但是它导致了相同的错误。
$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -Credential $credential -ContentType application/json-patch+json -InFile E:\scripts\JsonTemplate.txt对我的儿子有什么意见吗?
谢谢!
发布于 2017-04-20 14:52:07
啊啊!我差点就到了。我偶然猜到,尽管文献资料看起来应该是错的,但attribrutes下面的双卷发括号是错误的。当我把那些拿掉的时候,效果很好。
现在我的儿子看起来是这样的:
[
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
"attributes":
{
"isLocked": false
}
}
}
]https://stackoverflow.com/questions/43522112
复制相似问题