简体   繁体   中英

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

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.

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

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 ?

You need to change $Projectname to [string[]]$Projectname , and then use ForEach statement. 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:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7

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