简体   繁体   English

Azure devops rest api 结果使用 powershell 脚本循环

[英]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.

相关问题 Azure DevOps REST API PowerShell 脚本中的顶部参数不起作用 - Azure DevOps REST API top parameter in PowerShell script is not working 使用 REST API in Z531B84AD41B2A753DA83ACF3 从 Azure DevOps Repo 获取文件 - Get file from Azure DevOps Repo Using REST API in Powershell Script 在PowerShell中使用Azure DevOps REST API更新内部版本定义 - Update build definition using Azure DevOps REST API in PowerShell 使用 Powershell 的 Azure Devops 审计 rest api 延续令牌 - Azure Devops audit rest api continuation token using powershell 如何使用 Devops REST API 搜索带有 Powershell 的 Azure Devops 代码存储库并输出到 CSV - How to search Azure Devops Code Repos w/ Powershell using Devops REST API and output to CSV 如何使用 PowerShell 从 Azure CloudShell 访问 Azure DevOps REST API? - How to access Azure DevOps REST API from Azure CloudShell using PowerShell? 根据Azure DevOps Rest API对Azure Pipelines Powershell任务进行身份验证 - Authenticate Azure Pipelines Powershell task against Azure DevOps Rest API 如何使用 powershell(Azure devops REST API)获取用户提交更改的 Azure devops git repo 文件夹名称 - How to get Azure devops git repo folder name where user committed changes using powershell (Azure devops REST API) 是否可以使用 Rest api Powershell 脚本执行 Azure 测试计划 - Is it possible to execute Azure Test Plans using Rest api Powershell Script Azure DevOps 使用 REST API 调用 GetItem - Azure DevOps calling GetItem using REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM