简体   繁体   中英

Convert Azure DevOps rest api test plans for all projects to csv file using powershell

I am trying to generate report for all the test plans available in azure DevOps organization. here is my script which generates list of projects and the iterate through every project to find test plans available in it. I want all this data to be saved in csv file. I am able to get json file. Is there any way I can get this data saved in csv file with every project iterate for test plan ?

$connectionToken = ""
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?  
api-versions=5.0"

$base64AuthInfo= 
 [System.Convert]::ToBase64String([System.Text.Encoding]
 '::ASCII.GetBytes(":$($connectionToken)"))

$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers     
@{authorization = "Basic $base64AuthInfo"} - 
Method Get

$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100

$Projectname = $ProjectInfo.value.name 

ForEach ($project in $Projectname){

$TestPlanApi = "https://dev.azure.com/{org}/$project/_apis/test/plans?   
api-version=5.0"

$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers   
@{authorization = "Basic $base64AuthInfo"} -Method Get

  if (-NOT ($TestplanInfo.count -eq 0)){   
   
   $info = $TestplanInfo | ConvertTo-Json -Depth 100

   $info

 }
 }

This gives me following json file , I want to convert in to csv. Due to every project test plans value starts the Json result with value I am not able to expand it and save to csv

 {
   "value": [
     {
       "id": 134,
        "name": "sprint1",
        "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
         tfvc/_apis/test/Plans/1",
         "project": {
            "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
             "name": "Fabrikam-Fiber-TFVC",
             "url":    
             "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-                         
             Fiber- 
                   TFVC"
           },
          "area": {
            "id": "343",
            "name": "Fabrikam-Fiber-TFVC"
           },
          "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
          "state": "Active",
          "rootSuite": {
            "id": "1"
          },
          "clientUrl": "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
          Fiber-TFVC/Testing/testplan/connect?id=1"
       }
     ],
     "count": 1
    }
    {
      "value": [
        {
          "id": 567,
          "name": "sprint1",
          "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
           tfvc/_apis/test/Plans/1",
          "project": {
             "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
             "name": "Fabrikam-Fiber-TFVC",
             "url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber- 
                    TFVC"
            },
            "area": {
               "id": "343",
               "name": "Fabrikam-Fiber-TFVC"
             },
             "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
             "state": "Active",
             "rootSuite": {
                "id": "1"
             },
             "clientUrl":"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
             Fiber-TFVC/Testing/testplan/connect?id=1"
          },
          {
          "id": 678,
          "name": "sprint1",
          "url": "https://dev.azure.com/fabrikam/fabrikam-fiber- 
             tfvc/_apis/test/Plans/1",
          "project": {
            "id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
            "name": "Fabrikam-Fiber-TFVC",
            "url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber- 
                 TFVC"
           },
           "area": {
             "id": "343",
             "name": "Fabrikam-Fiber-TFVC"
           },
           "iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
           "state": "Active",
           "rootSuite": {
             "id": "1"
           },
           "clientUrl": 
           "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam- 
            Fiber-TFVC/Testing/testplan/connect?id=1"
         }
        ],
        "count": 2
       }

These are the values for different projects test runs, some projects have count = 1 then it shows one id , some projects has count =3 then it shows all the 3 ids and so on

I want this json file in csv file with columns -

id , name , url , project.name , project.id , project.url ,area.id , area.name , iteration , owner , revision , state , rootsuite.id , clienturl

How can I expand all the values in csv file ? I tried

Select-object Expand-property value but its fails to to expand all the values in json data

As a workaround, we can save the response body and then convert the json file to csv file.

We cannot save all info in one csv file, the latest info will overwrite the old data, so we need to save the test plan info in different csv files.

Sample:

$connectionToken = "{PAT}"
$BaseUrl = "https://dev.azure.com/{Org}/_apis/projects?api-version=6.1-preview.4"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100

$Projectname = $ProjectInfo.value.name 

ForEach ($project in $Projectname){

$TestPlanApi = "https://dev.azure.com/{Org}/$project/_apis/test/plans?api-version=5.0"

$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get 
if (-NOT ($TestplanInfo.count -eq 0)){       
   
   #Save the test plan info to json file
   $info = $TestplanInfo | ConvertTo-Json -Depth 100 | out-file E:\test\$project.json
   #convert the json file to csv file 
   Get-Content -Path E:\test\$project.json  | ConvertFrom-Json | Select-Object -expand value | ConvertTo-Csv -NoTypeInformation | Set-Content E:\test\$project.csv
   #delete json file
   Remove-Item E:\test\$project.json
 }
 }

Result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM