[英]Azure devops rest api result looping using powershell script
I am working on script where I want to generate csv report for test plans for all azure devops project in organization我正在编写脚本,我想为组织中的所有 azure devops 项目生成测试计划的 csv 报告
I have below script which first generates name of all project in selected organization , I want loop in this script which can take project name one by one from my list and executes test plan api and generate output , which I can save in csv file.我有下面的脚本,它首先生成选定组织中所有项目的名称,我想在这个脚本中循环,它可以从我的列表中一个一个地获取项目名称并执行测试计划 api 并生成输出,我可以将其保存在 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
This gives me list of all projects like -
a
b
c
d
now I want to pass these projects one by one in below api -现在我想在下面的api中一一传递这些项目-
TastPlanApi = "https://dev.azure.com/{orgName}/{ProjectName}/_apis/test/plans?api-version=5.0"
$TestplanInfo = Invoke-RestMethod -Uri $TestplanApi -Headers @{authorization = "Basic
$base64AuthInfo"} - Method Get
If I provide projectname hardcoded then it gives me required value like -如果我提供硬编码的项目名称,那么它会为我提供所需的值,例如 -
a
value count
---- -----
{} 0
Is there any way I can have these printed for all projects together and saved in csv file with project name , value and count / also only projects which have value other than 0 ?有什么方法可以将所有项目的这些打印在一起并保存在带有项目名称、值和计数的 csv 文件中/也只有值不为 0 的项目吗?
You need to change $Projectname
to [string[]]$Projectname
, and then use ForEach
statement.您需要将
$Projectname
更改$Projectname
[string[]]$Projectname
,然后使用ForEach
语句。 Sample code as below:示例代码如下:
[string[]]$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)){
$testplanvalue = $TestplanInfo.value.name
$testplancount = $TestplanInfo.count
}
}
Then what you need to do is exporting $project
, $testplanvalue
, $testplancount
to csv:然后你需要做的是将
$project
、 $testplanvalue
、 $testplancount
导出到 csv:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.