You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Postman调用Azure DevOps迭代接口正常,PowerShell返回空值及过滤疑问

问题分析与解决

一、PowerShell调用返回空值的原因

你的PowerShell脚本中请求头存在错误:Content='application/json' 的键名不正确,正确的HTTP请求头键名应为 Content-Type。Postman会自动正确设置该请求头,而脚本里的错误键名导致请求格式不符合接口要求,因此返回空值。

修正后的PowerShell脚本

$sourceBase64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName, $SourceToken)))
$Header = @{
    Authorization = ("Basic {0}" -f $sourceBase64AuthInfo)
    Content-Type='application/json'  # 修正此处的键名
}
$SourceURL = 'https://dev.azure.com/{0}/{1}/_apis/wit/classificationnodes/Iterations?$depth=2&api-version=6.0' -f $SourceOrg, $SourceProject

Try {
    $SourceIterations = (Invoke-RestMethod $SourceURL -Headers $Header).value
}
Catch {
    if ($_ -match "Access Denied") {
        Throw "Access has been denied, please check your token"
    }
    else {
        Throw $_
    }
}

二、过滤结果仅保留迭代/领域数据的方法

如果暂时无法使用/Iterations或/Areas路径,可以先获取全部分类节点数据,再通过PowerShell命令筛选目标内容:

1. 获取所有分类节点并筛选迭代数据

$sourceBase64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName, $SourceToken)))
$Header = @{
    Authorization = ("Basic {0}" -f $sourceBase64AuthInfo)
    Content-Type='application/json'
}
$SourceURL = 'https://dev.azure.com/{0}/{1}/_apis/wit/classificationnodes?$depth=2&api-version=6.0' -f $SourceOrg, $SourceProject

Try {
    $allClassificationNodes = Invoke-RestMethod $SourceURL -Headers $Header
    # 仅保留Iterations节点及其子项
    $onlyIterations = $allClassificationNodes.value | Where-Object { $_.name -eq 'Iterations' }
    # 若需要获取迭代的子节点,可访问 $onlyIterations.children
}
Catch {
    if ($_ -match "Access Denied") {
        Throw "Access has been denied, please check your token"
    }
    else {
        Throw $_
    }
}

2. 筛选领域(Areas)数据

将上述过滤条件修改为$_.name -eq 'Areas'即可:

$onlyAreas = $allClassificationNodes.value | Where-Object { $_.name -eq 'Areas' }

内容的提问来源于stack exchange,提问作者USMC6072

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 08:09:51