【发布时间】:2023-03-17 21:12:02
【问题描述】:
我目前在 Azure DevOps 和 PowerShell 工作。我需要使用 PowerShell 通过 API 构建一个 azure 管道。我已经做了一些事情来列出我组织中的项目。请帮助我使用 PowerShell 类似地通过 API 构建管道。
function GetUrl() {
param(
[string]$orgUrl,
[hashtable]$header,
[string]$AreaId
)
# Build the URL for calling the org-level Resource Areas REST API for the RM APIs
$orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1",
$orgUrl, $AreaId)
# Do a GET on this URL (this returns an object with a "locationUrl" field)
$results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header
# The "locationUrl" field reflects the correct base URL for RM REST API calls
if ("null" -eq $results) {
$areaUrl = $orgUrl
}
else {
$areaUrl = $results.locationUrl
}
return $areaUrl
}
$orgUrl = "https://dev.azure.com/<my organization>/"
$personalToken = "<my path>"
Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token =
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$coreAreaId = "79134c72-4a58-4b42-976c-04e7115f32bf"
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $coreAreaId
$projectsUrl = "$($tfsBaseUrl)_apis/projects?api-version=5.0"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -
Headers $header
$projects.value | ForEach-Object {
Write-Host $_.name
}
在运行此代码时,我的输出列出了我的项目。
【问题讨论】:
标签: powershell azure-pipelines