簡體   English   中英

使用 powershell 將所有項目的 Azure DevOps rest api 測試計划轉換為 csv 文件

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

我正在嘗試為 azure DevOps 組織中可用的所有測試計划生成報告。 這是我的腳本,它生成項目列表並遍歷每個項目以查找其中可用的測試計划。 我希望所有這些數據都保存在 csv 文件中。 我能夠獲得json文件。 有什么辦法可以將這些數據保存在 csv 文件中,並且每個項目都針對測試計划進行迭代?

$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

 }
 }

這給了我以下 json 文件,我想轉換為 csv。 由於每個項目測試計划值都以值開始 Json 結果,我無法擴展它並保存到 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
       }

這些是不同項目測試運行的值,有些項目有 count = 1 然后顯示一個 id ,有些項目有 count =3 然后顯示所有 3 個 id 等等

我想要這個帶有列的 csv 文件中的 json 文件 -

id、name、url、project.name、project.id、project.url、area.id、area.name、迭代、所有者、修訂、狀態、rootsuite.id、clienturl

如何擴展 csv 文件中的所有值? 我試過

選擇對象展開屬性值,但無法展開json數據中的所有值

作為一種解決方法,我們可以保存響應正文,然后將 json 文件轉換為 csv 文件。

我們無法將所有信息保存在一個 csv 文件中,最新的信息會覆蓋舊數據,因此我們需要將測試計划信息保存在不同的 csv 文件中。

樣本:

$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
 }
 }

結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM