使用 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 等等
我想要这个 json 文件在 csv 文件中,列为 -
id , name , url , project.name , project.id , project.url ,area.id , area.name , iteration , owner , revision , 状态 , rootsuite.id , 客户端url
如何展开 csv 文件中的所有值?我试过了
Select-object Expand-属性 value 但无法扩展 json data
中的所有值
作为解决方法,我们可以保存响应正文,然后将 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
}
}
结果:
我正在尝试为 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 等等
我想要这个 json 文件在 csv 文件中,列为 -
id , name , url , project.name , project.id , project.url ,area.id , area.name , iteration , owner , revision , 状态 , rootsuite.id , 客户端url
如何展开 csv 文件中的所有值?我试过了
Select-object Expand-属性 value 但无法扩展 json data
中的所有值作为解决方法,我们可以保存响应正文,然后将 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
}
}
结果: