For development purpose we need to have a PowerShell script launched locally via Visual Studio which task is to get the last build id of a specific branch.
So far I have tried many options based on this:
$WebClient = New-Object Net.WebClient
Write-Host "Downloading patches and binaries"
Write-Host "Get ID"
$url = "https://oldrepo.visualstudio.com/ProjectA/_apis/build/latest/14?branchName=master"
$result = $WebClient.DownloadString($url)
Write-Host $result
However the result
value returns the login page of azure and not my repository. If I paste the same url in a browser however I do get the good page.
I suppose that there's some sort of cookie / AD credentials passed through the request but I haven't seen anything relevant via developer tools.
I tried to manually set credentials to the WebClient object like this:
#$creds = Get-Credential -UserName "User" -Message "Login"
#$WebClient.Credentials = $creds
#$WebClient.UseDefaultCredentials = $true
But to no avail. What am I missing?
I use Invoke-RestMethod
to get Azure DevOps Rest API response, and you need to authenticate with Personal Access Token :
$token = "YOUR-PAT"
$base64Auth = [Convert]::ToBase64String([Test.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))
$header = "@{Authorization=("Basic {0}" -f $base64Auth)
$url = "https://oldrepo.visualstudio.com/ProjectA/_apis/build/latest/14?branchName=master"
$result = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json -Headers $header
Shayki's answer works but the latest API endpoints have changed more details here
and please find the updated query below
$token = "YOUR-PAT"
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$token)))
$header = @{Authorization=("Basic {0}" -f $base64Auth)}
$url = "GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definition}?api-version=7.0-preview.1"
$result = Invoke-RestMethod -Uri $url -Method Get -ContentType application/json -Headers $header
$result.id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.