Search code examples
azurepowershellazure-devopsazure-pipelinespipeline

Loop Issue In PowerShell?


I'm attempting to pull some data from various Azure API endpoints and not getting the exact results that I need I'm sure because of how I'm attempting to utilize loops. I'm pulling all project names correctly but not hitting "get pipelines runs" correctly to attempt to pull the id and individual times and the id of the last runs of each pipeline in AzDo. The $id is and times are both arrays since it captures multiple runs hence the "[0]" to grab the first in the index. What can I do to clean this up and make it more efficient...and actually work. In this state it only produces the list of projects successfully.

$personalToken = "****************************"
 
#Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token" }

# Get projects 
$projecturl = "https://dev.azure.com/*******/_apis/projects?api-version=6.0"
$projects = Invoke-RestMethod -Uri $projecturl -Method Get -ContentType "application/json" -Headers $header

# Get pipeline runs
$runurl = "https://dev.azure.com/*********/$projectname/_apis/pipelines/$id/runs?api-version=6.0-preview.1"
$runs = Invoke-RestMethod -Uri $runurl -Method Get -ContentType "application/json" -Headers $header

foreach($project in $projects.value) {
    Write-Output $project.name
    $projectname = $project.name

foreach($run in $runs.value.pipeline.id) {
    Write-Output $runs.value.pipeline.id
    $lastrundate = $runs.value.pipeline.id
    $id = $runs.value.pipeline.id[0]

# Builds API call
$url = "https://dev.azure.com/********/$projectname/_apis/pipelines?api-version=6.0-preview.1"
$output = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header
$output.value | Sort-Object id -Descending | ForEach-Object {
        Write-Host $_.name - $_.folder - $_.id - $_._links.web

        $obj = New-Object System.Object
        $obj | Add-Member -type NoteProperty -name LastRunDate -Value $lastrundate
        $obj | Add-Member -type NoteProperty -name ProjectName -Value $projectname
        $obj | Add-Member -type NoteProperty -name PipelineName -Value $_.name
        $obj | Add-Member -type NoteProperty -name PipelineFolder -Value $_.folder 
        $obj | Add-Member -type NoteProperty -name PipelineURL -Value $_._links.web
        $obj | Add-Member -type NoteProperty -name PipelineID -Value $_.id
        $obj | Select-Object LastRunDate, ProjectName, PipelineName , PipelineFolder , PipelineURL , PipelineID | Export-Csv -Append c:\Temp\****.csv
       }
    }

Solution

  • $personalToken = "xx"
     
    #Write-Host "Initialize authentication context" -ForegroundColor Yellow
    $token = [System.Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
    $header = @{authorization = "Basic $token" }   
    
    # Get projects 
    $projecturl = "https://dev.azure.com/xxx/_apis/projects?api-version=6.0"
    $projects = Invoke-RestMethod -Uri $projecturl -Method Get -ContentType "application/json" -Headers $header
           
    foreach($project in $projects.value) {
        #Write-Output $project.name
        $projectname = $project.name
    
        # List pipeline ID via org name and project name
        $url = "https://dev.azure.com/xxx/$($projectname)/_apis/pipelines?api-version=6.0-preview.1"
        $pipelines = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers $header   
    
        foreach($pipeline in $pipelines.value){
            Write-Host $pipeline.id
            $pipelineId = $pipeline.id
            #List run details via pipeline id and get the latest run info
            $runrul = "https://dev.azure.com/xxx/$($projectname)/_apis/pipelines/$($pipelineId)/runs?api-version=6.0-preview.1"
            $runResult = Invoke-RestMethod -Uri $runrul -Method Get -ContentType "application/json" -Headers $header
            $output = $runResult.value[0]
            $runID = $output.id
            $lastrundate = $output.createdDate
            $pipelineName = $output.pipeline.name
            $pipelineFolder = $output.pipeline.folder
            $pipelineURL = $output.pipeline.url
            $pipelineId
            #Write-Host $runID $lastrundate $pipelineName $pipelineFolder $pipelineURL $pipelineId
            $output = New-Object -TypeName PSObject -Property @{
                runID = $output.id
                lastrundate = $output.createdDate
                pipelineName = $output.pipeline.name
                pipelineFolder = $output.pipeline.folder
                pipelineURL = $output.pipeline.url
                pipelineId = $pipelineId
              } | Select-Object runID, lastrundate,pipelineName,pipelineFolder,pipelineURL,pipelineId
            $output | Export-Csv E:\test\testx.csv -Append -Force
        }
    }
    

    Test result:

    enter image description here