简体   繁体   English

如何在 Powershell 中使用 RestApi 从 AzureDevOps 获取工作项

[英]How to get the workitems from AzureDevOps with RestApi in Powershell

 Param(
      [string]$collectionurl = "https://dev.azure.com",
      [string]$project = "projectname",
      [string]$token = "PAT"
       )

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

         $baseUrl = 
         "$collectionurl/$project/_apis/wit/reporting/workitemrevisions? 
         includeLatestOnly=true&api-version=5.0-preview.2"         
           $response = (Invoke-RestMethod -Uri $baseUrl -Method Get - 
           UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f 
           $base64AuthInfo)}).values
           $wits = $response | where({$_.fields.'System.WorkItemType' -eq 
           'Task'}) # Only retrieve Tasks

           $witrevisions = @()

           foreach($wit in $wits){

           $customObject = new-object PSObject -property @{
           "WitID" = $wit.fields.'System.Id'   
           "rev" = $wit.fields.'System.Rev'
           "Title" = $wit.fields.'System.Title'
           "AssignedTo" = $wit.fields.'System.AssignedTo'
           "ChangedDate" = $wit.fields.'System.ChangedDate'
           "ChangedBy" = $wit.fields.'System.ChangedBy'
           "WorkItemType" = $wit.fields.'System.WorkItemType'
             } 

           $witrevisions += $customObject      
             }

           $witrevisions | Select-Object `
                WitID,
                rev,
                Title, 
                AssignedTo,
                ChangedDate, 
                ChangedBy,
                WorkItemType #|export-csv -Path E:\ashwin\devdata.csv - 
                 NoTypeInformation


                 Write-Output $witrevisions

I want to display the workitems in my project to be displayed using powershell with the following Rest Api using my PAT.我想在我的项目中显示要使用 powershell 显示的工作项,并使用我的 PAT 使用以下 Rest Api。

https://dev.azure.com/ {organization}/{project}/_apis/wit/workitems/{id}?api-version=5.1 https://dev.azure.com/ {organization}/{project}/_apis/wit/workitems/{id}?api-version=5.1

How to get the workitems from AzureDevOps with RestApi in Powershell如何在 Powershell 中使用 RestApi 从 AzureDevOps 获取工作项

The result will display in the output, you will find like following:结果将显示在输出中,您会发现如下所示:

在此处输入图片说明

If you do not find above output, make sure you have workitem with Task type, because you have set the condition 'System.WorkItemType' -eq 'Task' in the powershell scripts.如果您没有找到以上输出,请确保您有任务类型的工作项,因为您已在 powershell 脚本中设置了条件'System.WorkItemType' -eq 'Task'

On the other hand, you could export the work item list to a *.csv file, this part of the code is commented in powershell:另一方面,您可以将工作项列表导出到*.csv文件,这部分代码在 powershell 中进行了注释:

WorkItemType #| export-csv -Path G:\temp\WIT.csv -NoTypeInformation

If you want to create a *.csv file, you need to remove the # in that line, it should be :如果要创建*.csv文件,则需要删除该行中的# ,它应该是:

WorkItemType | export-csv -Path G:\temp\WIT.csv -NoTypeInformation

Now, we could get that file in our local folder:现在,我们可以在本地文件夹中获取该文件:

在此处输入图片说明

Note: The path is G:\\temp is a local path, you should use the private agent, if you are using the hosted agent, you should copy that file from the hosted agent, and publish it to pipeline artifact.注意:路径G:\\temp是本地路径,您应该使用私有代理,如果您使用的是托管代理,您应该从托管代理复制该文件,并将其发布到管道工件。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 AWS 工具使用来自 AzureDevops 的 Powershell 7 - How to use Powershell 7 from AzureDevops with AWS Tools 从 Azure DevOps 获取 WorkItems - Get WorkItems from Azure DevOps 如何使用 AzureDevOps 管道和 PowerShell 从 Azure Repos git 下载文件 - How to download file from Azure Repos git using AzureDevOps pipelines && PowerShell 纽曼与 AzureDevOps + Powershell - Newman with AzureDevOps + Powershell AzureDevops PowerShell和任务 - AzureDevops PowerShell and Tasks 使用 PowerShell 进行 RestAPI 身份验证 - RestAPI authentication with PowerShell 如何使用PowerShell Where-Object返回未分配工作项的TFS变更集? - How to use PowerShell Where-Object to return TFS changesets that have no workitems assigned? 在 AzureDevops 中,尝试运行一个 yaml 文件来触发一个 powershell 脚本——我如何为脚本分配不同的环境值 - In AzureDevops , trying to run a yaml file to trigger a powershell script - how can i assign different environment values to script 如何从powershell获取主机名? - how to get the HostName from powershell? 尝试从Powershell调用GitHub RestApi调用以创建Git树,并获取“问题解析JSON” - Trying to invoke GitHub RestApi call from Powershell to create a Git Tree and getting “Problems parsing JSON”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM