【发布时间】:2017-09-17 05:46:19
【问题描述】:
我正在尝试使用 powershell 和 json 将父链接添加到 TFS 工作项。我们有一个内部 TFS 服务器(即,不是团队服务)。
我得到了我的查询的答案,所以我与 TFS 的连接正常,但是当我尝试更新时,我收到以下错误:
"You must pass a valid patch document in the body of the request."
我是一个 json 菜鸟,我从这个 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 有什么问题有什么想法吗?
谢谢!
【问题讨论】:
标签: json powershell tfs-workitem