首页 > 解决方案 > Azure DevOps API:powershell 获取测试点列表

问题描述

有没有办法通过 Azure DevOps API 获取测试点数据列表?

列表

我试过这个powershell脚本

function GetUrl() {
    param(
        [string]$orgUrl, 
        [hashtable]$header, 
        [string]$AreaId
    )

    # Area ids
    # https://docs.microsoft.com/en-us/azure/devops/extend/develop/work-with-urls?view=azure-devops&tabs=http&viewFallbackFrom=vsts#resource-area-ids-reference
    # 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/fodservices"
$personalToken = "<my token pat>"

Write-Host "Initialize authentication context" -ForegroundColor Yellow
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}

Write-Host "Demo 3"

$coreAreaId = "3b95fb80-fdda-4218-b60e-1052d070ae6b"
$tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $coreAreaId

$relDefUrl = "$($tfsBaseUrl)/_apis/testplan/Plans/70152/Suites/70154/TestPoint?api-version=5.0-preview.2"
try {
    $output = Invoke-RestMethod -Uri $relDefUrl -Method Get -ContentType "application/json" -Headers $header

}
catch{
    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription

}
$output.value | ForEach-Object {
    Write-Host $_.id
}

结果是:

演示 3

状态码:404

状态描述:未找到

谁能告诉我我在使用 powershell 和 azure devops rest api 时做错了什么

标签: powershellazure-devopsazure-devops-rest-api

解决方案


查看$tfsBaseUrl变量的内容。它包括组织名称,但不包括项目名称。您需要在 URL 中包含项目名称。查看文档并将您的 URL 与文档的 URL 进行比较。


推荐阅读