Fetch the latest build number which is successful from azure devops using powershell

Using the following script I am getting the build information from the pipeline and want to extract the latest build number which is succeed,

function fetchBuildNumber() {
        $pat ="<personal acess token>"
        $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
        $UriOrganization = "https://<rest_api>/_apis/build/builds?api-version=7.1-preview.7"  
        $ProjectsResult = Invoke-RestMethod -Uri $UriOrganization -Method get -Headers $AzureDevOpsAuthenicationHeader
    
        Foreach ($buildResult in $ProjectsResult.value.result) {
            if ($buildResult.equals("succeeded")){
                Write-Host $ProjectsResult.value.buildNumber
             }
        }
    }
    
    fetchBuildNumber

I am getting the below result,

@{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223567; buildNumber=20231016.1; status=completed; result=succeeded; queueTime=2023-10-16T06:23:50.9273616Z; startTime=2023-10-16T06:23:58.4673447Z; finishTime=2023-10-16T06:24:10.4163032Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223567; definition=; buildNumberRevision=1; project=; uri=vstfs:///Build/Build/223567; sourceBranch=refs/heads/master; sourceVersion=959c15fdf53e30a60ab9f1f95f050c07380248d6; queue=; priority=normal; reason=individualCI; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T06:24:10.81Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True} @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223566; buildNumber=20231016.5; status=completed; result=failed; queueTime=2023-10-16T06:22:53.8662857Z; startTime=2023-10-16T06:23:01.2164532Z; finishTime=2023-10-16T06:23:16.1320749Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223566; definition=; buildNumberRevision=5; project=; uri=vstfs:///Build/Build/223566; sourceBranch=refs/heads/master; sourceVersion=482cfa62cea136463b7c8f10ccf174d9d78d7349; queue=; priority=normal; reason=individualCI; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T06:23:16.603Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True} @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223556; buildNumber=20231016.4; status=completed; result=failed; queueTime=2023-10-16T05:44:36.5531704Z; startTime=2023-10-16T05:44:43.9888586Z; finishTime=2023-10-16T05:45:00.6769804Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223556; definition=; buildNumberRevision=4; project=; uri=vstfs:///Build/Build/223556; sourceBranch=refs/heads/master; sourceVersion=7c3708310a88b1074c5f455dd0a896ee2c532cf0; queue=; priority=normal; reason=manual; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T05:45:00.933Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True} @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223555; buildNumber=20231016.3; status=completed; result=failed; queueTime=2023-10-16T05:44:30.908171Z; startTime=2023-10-16T05:44:37.2857315Z; finishTime=2023-10-16T05:44:50.3619703Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223555; definition=; buildNumberRevision=3; project=; uri=vstfs:///Build/Build/223555; sourceBranch=refs/heads/master; sourceVersion=7c3708310a88b1074c5f455dd0a896ee2c532cf0; queue=; priority=normal; reason=individualCI; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T05:44:50.69Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True} @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223552; buildNumber=20231016.2; status=completed; result=succeeded; queueTime=2023-10-16T05:38:42.0775965Z; startTime=2023-10-16T05:38:49.9645514Z; finishTime=2023-10-16T05:39:00.9177615Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223552; definition=; buildNumberRevision=2; project=; uri=vstfs:///Build/Build/223552; sourceBranch=refs/heads/master; sourceVersion=01eae21771b801f4593aaf2605a017c86b93f45c; queue=; priority=normal; reason=individualCI; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T05:39:01.27Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True} @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=223551; buildNumber=20231016.1; status=completed; result=succeeded; queueTime=2023-10-16T05:37:38.8167652Z; startTime=2023-10-16T05:37:46.3075953Z; finishTime=2023-10-16T05:37:58.4518333Z; url=https://<url>/<project>/b9f9d6e9-a99a-4c65-9c44-adf670a6fff3/_apis/build/Builds/223551; definition=; buildNumberRevision=1; project=; uri=vstfs:///Build/Build/223551; sourceBranch=refs/heads/master; sourceVersion=1d793ebd727867f1f0dfa76a569c593463236ce9; queue=; priority=normal; reason=individualCI; requestedFor=; requestedBy=; lastChangedDate=2023-10-16T05:37:58.893Z; lastChangedBy=; orchestrationPlan=; logs=; repository=; retainedByRelease=False; triggeredByBuild=; appendCommitMessageToRunName=True}

How to fetch the latest build number which is succeeded (which is "20231016.5" ) what changes required in my code ?

Jyoti,
Welcome back to the forum. :wave:t3: … long time no see.

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you potentially wasting their time.

Thanks

EDIT: the reason yours doesn’t work is due to the loop. You’re looping through each result and if it succeeds, you’re outputting the entire $ProjectsResult.value.buildnumber each time, not just that specific build result.

This should work:

$pat = "InsertPatHere"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
$UriOrganization = "https://dev.azure.com/{Org}/{Project}/_apis/build/builds?api-version=7.2-preview.2"  
$ProjectsResult = Invoke-RestMethod -Uri $UriOrganization -Method get -Headers $AzureDevOpsAuthenicationHeader

$ProjectsResult.value | ForEach-Object {
    if ($_.result -eq 'succeeded') {
        Write-Host $_.buildNumber
    }
}

I adjusted the logic a bit and also am using a more current version of the endpoint because… may as well :slight_smile: .

May look into a better way to handle the PAT though. I didn’t change the write-host either but if you’re just wanting it to return to your console then it should be ok. However, if you actually want to do something with it, you probably should just drop it on the pipeline (AKA remove the write-host)

I also X-Posted an answer on stack overflow, somewhat shortened version.