简体   繁体   English

在本地使用 PowerShell 在分支上获取 Azure DevOps last build id

[英]Use PowerShell locally to get Azure DevOps last build id on branch

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.出于开发目的,我们需要通过 Visual Studio 在本地启动一个 PowerShell 脚本,该脚本的任务是获取特定分支的最后一个构建 ID。

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.但是result值返回 azure 的登录页面而不是我的存储库。 If I paste the same url in a browser however I do get the good page.但是,如果我在浏览器中粘贴相同的 url,我会得到好的页面。

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.我想通过请求传递了某种 cookie / AD 凭据,但我没有通过开发人员工具看到任何相关内容。

I tried to manually set credentials to the WebClient object like this:我尝试像这样手动将凭据设置为 WebClient object:

#$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 :我使用Invoke-RestMethod来获取 Azure DevOps Rest API 响应,您需要使用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 Shayki 的答案有效,但最新的 API 端点已在此处更改了更多详细信息

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM