1
votes
 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.

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

1
What have you tried and what did you observe?A. Kootstra
I followed the link stackoverflow.com/questions/50499392/get-workitems-from-vsts , where as i did'nt got any output nor error, I am new to power shell script.Ashwin Kumar
Can you please edit your question and add the steps/command you executed and your observations. Referring to another question/answer does very little in this regard.A. Kootstra
I edited the question with what I tried. @A.KootstraAshwin Kumar
Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.Leo Liu-MSFT

1 Answers

1
votes

How to get the workitems from AzureDevOps with RestApi in Powershell

The result will display in the output, you will find like following:

enter image description here

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.

On the other hand, you could export the work item list to a *.csv file, this part of the code is commented in 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 :

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

Now, we could get that file in our local folder:

enter image description here

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.

Hope this helps.