【发布时间】:2026-01-27 05:40:01
【问题描述】:
我想从 Azure devops 安全刀片获取组列表。我准备了一个代码。我是 Azure Devops 贡献者组的成员,我正在使用 cmd-let Invoke-RestMethod。我正在测试从笔记本电脑连接到我的 Azure 帐户的这段代码,而不是在 Azure 自动化或 Azure 管道上进行测试。我仍然面临一个问题->下面的错误消息: Invoke-RestMethod : 远程服务器返回错误:(401) Unauthorized.
##My Function
function GetUrl() {
param(
[string]$orgUrl,
[hashtable]$header,
[string]$AreaId
)
$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
}
$token =[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$header = @{authorization = "Basic $token"}
$orgUrlAD = "https://vsaex.dev.azure.com/OrganizationName"
$personalToken = "MyPersonalToken"
##Function execution
Write-Host "AD tests"
$coreAreaId = "xxx"
$tfsBaseUrl = GetUrl -orgUrl $orgUrlAD -header $header -AreaId
$coreAreaId
$projectsUrl = "$($tfsBaseUrl)_apis/groupentitlements?api-version=5.0-preview.1"
$projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header
$projects.value | ForEach-Object {
Write-Host $_.name
}
Invoke-RestMethod : 远程服务器返回错误: (401) 未经授权。
【问题讨论】:
标签: azure-devops azure-powershell azure-devops-rest-api