DevOpsHandling/Get-LastSuccessfulBuildId.ps1

<#
 .Synopsis
  Retrieves the id for the last successful build
 .Description
  Retrieves the id for the last successful build
 .Parameter devOpsOrganization
  The DevOps organization that contains the artifacts
  .Parameter devOpsProjectName
  The name of the DevOps project
  .Parameter repositoryName
  The name of the repository
  .Parameter devOpsToken
  PAT for DevOps
  .Example
  Get-LastSuccessfulBuildId -devOpsOrganization "test" -devOpsProject "test project" -repositoryName "test repo" -devOpsToken "00000000-0000-0000-0000-000000000000"
#>

function Get-LastSuccessfulBuildId {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $devOpsOrganization,
        [Parameter(Mandatory=$true)]
        [string] $devOpsProjectName,
        [Parameter(Mandatory=$true)]
        [string]$repositoryName,
        [Parameter(Mandatory=$true)]
        [string] $devOpsToken
    )

    try {
        $build = (Invoke-AzureDevOpsApi -url ('https://dev.azure.com/{0}/{1}/_apis/build/builds/?repositoryId={2}&repositoryType=TfsGit&resultFilter=succeeded&queryOrder=finishTimeDescending&$top=1' -f $devOpsOrganization, $devOpsProjectName, (Get-RepositoryId -devOpsOrganization $devOpsOrganization -devOpsProjectName $devOpsProjectName -repositoryName $repositoryName -devOpsToken $devOpsToken)) -devOpsToken $devOpsToken).value

        return $build.id
    }
    catch {
        return ""
    }
}