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
相关产品推荐
相关产品推荐

