DevOpsHandling/Get-AllSuccessfulBuilds.ps1

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

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

        return $builds
    }
    catch {
        return @()
    }
}
Export-ModuleMember Get-AllSuccessfulBuilds