Powershell

时间:2016-11-08 04:16:48

标签: excel powershell azure-devops

PowerShell中使用VSTS的REST API,我想在我的VSTS团队项目中创建一个报告,其中包含特定"迭代路径&#34下所有工作项的工作项链接关系的详细信息;和"区域路径"。

Ex:Epics→Features→UserStories。由于Epics& amp;之间存在父/子关系。功能以及功能和功能之间UserStories。

因此输入将是Iteration PathArea Path,相应的输出将是一个报告(.csv或.xls),其中包含这些工作项及其关系的所有详细信息。

有人可以告诉我如何使用PowerShell中的REST API实现这一目标吗?

我编写了用于在团队项目中获取分层工作项的代码,但需要修改以根据给定的迭代和区域路径过滤列表。 此外,在执行以下代码时,查询中的字符串比较(由于存在'')在power-shell中返回错误。

错误: Invoke-RestMethod:{" count":1," value":{" Message":&#34 ;解析值后,遇到意外字符:G。 路径'查询',第1行,第163位。\ r \ n"}}

$vstsAccount = "xxxx"
$projectName = "xxxx"
$user = "xxxx"
$token = "xxxx"

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

# Construct the REST URL to obtain Build ID
$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/wit/wiql?api-version=1.0"

$body = "{'query': 'Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] From WorkItemLinks WHERE 
((Source.[System.TeamProject] = '$projectName' and Source.[System.State] <> 'Removed') and (Source.[System.WorkItemType] = 'Epic') and
([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') and (Target.[System.WorkItemType] = 'UserStory') mode(Recursive)'}"

# Invoke the REST call and capture the results (notice this uses the POST method)
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body

1 个答案:

答案 0 :(得分:1)

修改部分代码:

    $body = @{query="Select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State] From WorkItemLinks WHERE 
    (Source.[System.TeamProject] = '$projectName' and Source.[System.State] <> 'Removed') and (Source.[System.WorkItemType] = 'Epic') and
    ([System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward') and (Target.[System.WorkItemType] = 'UserStory') mode (Recursive)"}
  $bodyJson=@($body) | ConvertTo-Json
    $result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $bodyJson