DevOpsHandling/Get-DevOpsArtifactsFromFeed.ps1

function Get-DevOpsArtifactsFromFeed {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$devOpsOrganization,
        [Parameter(Mandatory=$true)]
        [string]$devOpsFeed,
        [Parameter(Mandatory=$true)]
        [string]$devOpsArtifact,
        [Parameter(Mandatory=$true)]
        [string]$devOpsToken,
        [Parameter(Mandatory=$true)]
        [string]$destination
    )

    $appsList = [System.Collections.ArrayList]@()

    try {
        $artifacts = (Invoke-AzureDevOpsApi -url ('https://feeds.dev.azure.com/{0}/_apis/packaging/Feeds/{1}/packages' -f $devOpsOrganization, $devOpsFeed) -devOpsToken $devOpsToken).value
    }
    catch {
        Write-Error "Could not find artifacts for organization $devOpsOrganization, feed $devOpsFeed, artifact $devOpsArtifact"
        throw $_.Exception.Message
    }
    
    foreach($artifact in $artifacts | Where-Object Name -eq $devOpsArtifact) {
        try {
            $versions = (Invoke-AzureDevOpsApi -url ('https://feeds.dev.azure.com/{0}/_apis/packaging/Feeds/{1}/Packages/{2}/versions?includeUrls-true&isDeleted=false' -f $devOpsOrganization, $devOpsFeed, $artifact.id) -devOpsToken $devOpsToken).value
            $version = ($versions | Where-Object isLatest -eq $true)

            $app = (Invoke-AzureDevOpsApi -url $version.url -destination $destination -outfile (Join-Path $destination "$($artifact.Name).app") -devOpsToken $devOpsToken)
            [void]$appsList.Add($app)
        }
        catch {
            Write-Error "Could not find versions for artifact $($artifact.Name), organization $devOpsOrganization, feed $devOpsFeed"
            throw $_.Exception.Message
        }
    }

    return $appsList
}
Export-ModuleMember Get-DevOpsArtifactsFromFeed