2017-02-07 26 views
1

當試圖更新使用調用-RestMethod現有VSTS工作項目,我不斷收到什麼是正確的調用,RestMethod語法來更新VSTS工作項目在PowerShell中 - 構建包含Windows路徑

JSON字符串您必須在請求的正文中傳遞有效的補丁文檔。

這裏是我傳遞

$Body = '[{ "op": "test", "path": "/rev", "value": 1},{ "op": "add", "path": "/fields/System.IterationPath", "value":"' + $caseIterationPath + '"},{ "op": "add", "path": "/fields/System.AreaPath", "value":"' + $caseAreaPath + '"}]' 

Invoke-RestMethod -Uri "$rootUri/_apis/wit/workitems/$($case.id)?$apiVersion" -Method PATCH -ContentType application/json-patch+json -Headers @{Authorization= $authorization} -Body $Body 

體膨脹到

"[{ "op": "test", "path": "/rev", "value": 1},{ "op": "add", "path": "/fields/System.IterationPath", "value":"Foo\Bar 2016.4\2016.4.02"},{ "op": "add", "path": "/fields/System.AreaPath", "value":"Foo\Apps\Bar Stool\Eating"}]" 

任何幫助將不勝感激!

回答

2

JSON使用\作爲轉義字符;例如,\t是TAB字符(Unicode代碼點0x9)的轉義序列。

因此,爲了使用字面\ - 如Windows路徑 - 你必須轉義爲\\

可以手動逃避所有您使用沿着​​(原文如此)的線合成的JSON字符串變量值。

但是,它可能是更容易構建第一定期PowerShell的數據結構,然後ConvertTo-Json需要做逃脫上的轉換。

在這種情況下,在手:

# Sample values. 
$caseIterationPath = 'c:\path\to\iteration' 
$caseAreaPath = 'c:\path\to\area' 

# Construct an array of hashtables to be converted to JSON later. 
$Body = @(

    @{ 
     op = 'test' 
     path = '/rev' 
     value = 1 
    } 

    @{ 
     op = 'add' 
     path = '/fields/System.IterationPath' 
     value = $caseIterationPath 
    } 

    @{ 
     op = 'add' 
     path = '/fields/System.AreaPath' 
     value = $caseAreaPath 
    } 

) 

# Convert the array to a string containing JSON. 
# ConvertTo-Json will perform any required escaping. 
$jsonBody = ConvertTo-Json $Body 

# Invoke the REST method. 
Invoke-RestMethod -Uri "$rootUri/_apis/wit/workitems/$($case.id)?$apiVersion" ` 
    -Method PATCH -ContentType application/json-patch+json ` 
    -Headers @{Authorization= $authorization} ` 
    -Body $jsonBody 

在上面的代碼,$jsonBody最終包含以下 - 注意\情況下如何進行正確轉義爲\\

[ 
    { 
     "path": "/rev", 
     "op": "test", 
     "value": 1 
    }, 
    { 
     "path": "/fields/System.IterationPath", 
     "op": "add", 
     "value": "c:\\path\\to\\iteration" 
    }, 
    { 
     "path": "/fields/System.AreaPath", 
     "op": "add", 
     "value": "c:\\path\\to\\area" 
    } 
] 
+1

謝謝!這是一個很大的幫助。 –

0

我想你可能需要在AreaPath的值中避開你的背斜線。從documentation的示例中,它們使用雙反斜槓。

{ 
    "id": 299, 
    "rev": 2, 
    "fields": { 
    "System.AreaPath": "Fabrikam-Fiber-Git\\Website", 
    "System.TeamProject": "Fabrikam-Fiber-Git", 
    "System.IterationPath": "Fabrikam-Fiber-Git", 
    "System.WorkItemType": "Task", 
    "System.State": "To Do", 
    "System.Reason": "New task", 
    "System.CreatedDate": "2014-12-29T20:49:21.617Z", 
    "System.CreatedBy": "Jamal Hartnett <[email protected]>", 
    "System.ChangedDate": "2014-12-29T20:49:23.933Z", 
    "System.ChangedBy": "Jamal Hartnett <[email protected]>", 
    "System.Title": "JavaScript implementation for Microsoft Account", 
    "Microsoft.VSTS.Scheduling.RemainingWork": 4, 
    "System.Description": "Follow the code samples from MSDN", 
    "System.History": "Moving to the right area path" 
    }, 
    "relations": [ 
    { 
     "rel": "System.LinkTypes.Hierarchy-Reverse", 
     "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/297", 
     "attributes": { 
     "isLocked": false, 
     "comment": "decomposition of work" 
     } 
    } 
    ], 
    "_links": { 
    "self": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/299" 
    }, 
    "workItemUpdates": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/299/updates" 
    }, 
    "workItemRevisions": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/299/revisions" 
    }, 
    "workItemHistory": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/299/history" 
    }, 
    "html": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/web/wi.aspx?pcguid=d81542e4-cdfa-4333-b082-1ae2d6c3ad16&id=299" 
    }, 
    "workItemType": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c/_apis/wit/workItemTypes/Task" 
    }, 
    "fields": { 
     "href": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/fields" 
    } 
    }, 
    "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/_apis/wit/workItems/299" 
} 
+0

謝謝!這是一個很大的幫助。 –

相關問題