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

PowerShell调用Azure DevOps REST API时top参数不生效问题咨询

问题原因

问题本质是PowerShell的字符串解析规则导致的:

  • PowerShell中双引号包裹的字符串会自动解析$开头的内容为变量,你直接在URL里写$top时,由于没有提前定义$top变量,PowerShell会将其替换为空值,最终实际发送的请求URL变成了https://dev.azure.com/uniperteamservices/_apis/projects?api-version=6.0&=500,不符合API参数要求,因此无正常返回。

正确修改方案

只需要对URL中的$字符用PowerShell转义符**反引号(`)**转义即可,避免其被识别为变量。

修改后的完整脚本示例:

$token="**************************************************************"
# 转义$top中的$符号,避免被PowerShell识别为变量
$url="https://dev.azure.com/{orgnization}/_apis/projects?api-version=6.0&`$top=500"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

Foreach($projectName in $response.value.name)
{
  # 如果后续获取发布记录的接口也需要返回更多条目,同理转义$top即可,示例如下:
  $url1="https://vsrm.dev.azure.com/{orgnization}/$($projectname)/_apis/release/releases?api-version=6.0&`$top=200"
 
  $response = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json-patch

   echo $response | ConvertTo-Json -Depth 99 |  Out-File "D:\\file.json" -Append
}

额外可选方案

你也可以选择用单引号包裹固定URL部分,单引号内的$不会被解析为变量:

$url = 'https://dev.azure.com/{orgnization}/_apis/projects?api-version=6.0&$top=500'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:24:04