2017-04-20 97 views
2

我想使用powershell和json添加一個父鏈接到TFS工作項。我們有一個內部的TFS服務器(即不是團隊服務)。更正json添加一個父鏈接到TFS工作項

我得到的回答我提出的質疑,所以我到TFS連接正常工作,但是當我嘗試更新,我得到以下錯誤:

"You must pass a valid patch document in the body of the request." 

我是一個JSON小白並得到了我的JSON骨架從這個MSDN page

這裏是我的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而得到那個錯誤。

我還測試了跳過獲取內容並使用-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

唉!我很親密。我偶然猜測,即使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 
    } 
    } 
} 
]