我正在尝试使用powershell和json将父链接添加到TFS工作项。我们有一个内部TFS服务器(即不是团队服务)。
我得到了我的查询的答案,所以我与TFS的连接正常,但是当我尝试更新时,我收到以下错误:
"You must pass a valid patch document in the body of the request."
我是一名json noob,并从MSDN page
获得了我的json骨架这是我的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
}
正如你所看到的,我已经注释掉了convertTo-json,因为我收到了这个错误: ConvertTo-Json:转换后的JSON字符串格式错误。 我不确定我是否收到了这个错误,因为它已经是json了。
我还测试了跳过get-content并使用-inFile参数但是它导致了同样的错误。
$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -Credential $credential -ContentType application/json-patch+json -InFile E:\scripts\JsonTemplate.txt
关于我的json有什么问题的任何想法?
谢谢!
答案 0 :(得分:0)
AARGH!我太近了。我偶然猜测,虽然the documentation看起来应该如此,但属性下的双花括号是错误的。当我删除那些它工作得很漂亮。
现在我的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
}
}
}
]