DevOpsHandling/Invoke-AzureDevOpsApi.ps1

function Invoke-AzureDevOpsApi {
    Param (
        [Parameter(Mandatory=$true)]
        [string]$url,
        [Parameter(Mandatory=$true)]
        [string] $devOpsToken,
        [Parameter(Mandatory=$false)]
        [string]$destination,
        [Parameter(Mandatory=$false)]
        [string]$outfile,
        [switch] $GetContents
    )

    if ($url -eq '' -or $null -eq $url) {
        return ''
    }
    
    # Authentication
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($devOpsToken)"))
    $header = @{authorization = "Basic $token"}

    # add API Version
    if (!$url.Contains('?')) {
        $url = ($url + '?api-version=5.1')
    }
    else {
        $url = ($url + '&api-version=5.1')
    }

    # If no out
    if ($outfile -eq '' -or $null -eq $outfile) {
        if ($GetContents.IsPresent) {
            $TempPath = (Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString() + ".TXT"))
            Invoke-RestMethod -Uri $url -Method Get -ContentType '*/*' -Headers $header -OutFile $TempPath
            $result = Get-Content $TempPath -Raw
            [IO.File]::Delete($TempPath)
            $result
        }
        else
        {
            $result = Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json' -Headers $header
            $result
        }
    }
    else {
        Invoke-RestMethod -Uri $url -Method Get -ContentType '*/*' -Headers $header -OutFile $outfile
        $fileList = [System.Collections.ArrayList]@()

        Expand-Archive $outfile -DestinationPath (Join-Path $destination ".zipfolder\") -Force
        $files = Get-ChildItem -Path (Join-Path $destination ".zipfolder\") -Recurse -File

        foreach ($file in $files) {
            [void]$fileList.Add((Copy-Item $file.FullName $destination -PassThru))
        }
        Remove-Item -Path (Join-Path $destination ".zipfolder") -Recurse -Force

        $fileList
    }
}
Export-ModuleMember Invoke-AzureDevOpsApi