Script to get build times from Azure DevOps

A little script I use for examining build job duration

param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string[]] $Project, [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $PAT ) $buildJobs = @() $Project | ForEach-Object { $ProjectName = $_ $uri = "https://dev.azure.com/<your-root-ADO-project>/$ProjectName/_apis/build/builds?api-version=5.0&resultFilter=succeeded&statusFilter=completed&buildNumber=*master*" $temp = $null $encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$PAT")) Invoke-RestMethod -Uri $uri -Headers @{Authorization = "Basic $encodedPat"} | Select-Object -ExpandProperty value | Where-Object {$_.definition.name -notmatch 't_'} | ForEach-Object { $BuildNum = $_.buildNumber # Get ADO Timeline data $timeLineUri = "https://dev.azure.com/<your-root-ADO-project>/$ProjectName/_apis/build/builds/$($_.Id)/timeline?api-version=5.1-preview.2" $TimeLine = Invoke-RestMethod -Uri $timeLineUri -Headers @{Authorization = "Basic $encodedPat"} | Select-Object -ExpandProperty Records | Select-Object Name, @{N="Time";E={$(New-TimeSpan -Start $_.startTime -End $_.finishTime)}} $ArtifactsUri = "https://dev.azure.com/<your-root-ADO-project>/$ProjectName/_apis/build/builds/$($_.Id)/artifacts?api-version=5.0" $Artifacts = Invoke-RestMethod -Uri $ArtifactsUri -Headers @{Authorization = "Basic $encodedPat"} $_ | ForEach-Object { $temp = [PSCustomObject]@{ Project = $ProjectName JobName = $_.definition.name BuildNumber = $BuildNum TotalBuildTime = "{0:dd-hh:mm:ss}" -f $(New-TimeSpan -Start $_.startTime -End $_.finishTime) DropLocation = $DropLocation Url = $_.url artifacts = $Artifacts } $i = 0 $TimeLine | ForEach-Object { $temp | Add-Member -MemberType NoteProperty -Name "$($_.Name)-$i" -Value $("{0:dd-hh:mm:ss}" -f $_.Time) $i++ } } $buildJobs += $temp } } Write-Output $buildJobs 

submitted by /u/deusletum
[link] [comments]

Leave a Reply