powershell - Azure DevOps 服务器调用 RestMethod 错误 : No API version provided for the "PUT" request

标签 powershell azure-devops azure-devops-rest-api

基于此PowerShell example来自 Etienne Tremblay 我尝试将数据传递到 Azure DevOps Server (2019.0.1) REST API。

在此示例中,我想修改项目的描述文本(仅出于测试原因):

# DEMO 5 Update an environement build variable
Write-Host "Demo 5"
$projects.value | ForEach-Object {
    $project = $_.name
    $buildManagementAreaId = "efc2f575-36ef-48e9-b672-0c6fb4a48ac5"
    $tfsBaseUrl = GetUrl -orgUrl $orgUrl -header $header -AreaId $buildManagementAreaId

    # https://learn.microsoft.com/en-us/rest/api/azure/devops/build/definitions/list?view=azure-devops-rest-5.0
    $relDefUrl = "$tfsBaseUrl$project/_apis/build/definitions?api-version=5.0"
    Write-Host "Requesting URL: $relDefUrl" -ForegroundColor Yellow
    $result = Invoke-RestMethod $relDefUrl -Method Get -ContentType "application/json" -Headers $header
    $relDefs = $result.value

    if($relDefs.count -gt 0){
        Write-Host "$project $($relDefs.count) build def founds" -ForegroundColor Blue
        $relDefs | ForEach-Object {

            $relDef = $_
            $relDefExpanded = Invoke-RestMethod "$($relDef.url)?`$Expand=Environments&api-version=5.0" -Method Get -ContentType "application/json" -Headers $header
            $relDefExpanded.project.description = "Hallo!!!"

            $body = $relDefExpanded | ConvertTo-Json -Depth 100 -Compress
            $body = [System.Text.Encoding]::UTF8.GetBytes($body)

            #ERROR OCCURS HERE
            $updateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Method Put -ContentType "application/json" -body $body -Headers $header 
            Write-host "Variable value after: $($updateResult.project.description)" -ForegroundColor Green
        }
    }
}

提示:

  • $projects 在脚本中之前定义(demo1)
  • 原始脚本更改了变量而不是描述(两个版本都不起作用)

错误发生于:

$updateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Method Put -ContentType "application/json" -body $body -Headers $header 

消息:

Invoke-RestMethod: {"$ id": "1", "innerException": null, "message": "No API version provided for the" PUT "request The version must either be part of the" Accept "header (eg
\ "application / json; api-version = 1.0 \") or as a query parameter (for example, "? api-version = 1.0")
. Will "," type name ":" Microsoft.VisualStudio.Services.WebApi.VssVersionNotSpecifiedException,
Microsoft.VisualStudio.Services.WebApi","typeKey":"VssVersionNotSpecifiedException","errorCode":0,"eventId":3000}
In C:\Users\mkober\Desktop\Azure DevOps Console\WriteAPI.ps1:75 Zeichen:29
+ ... ateResult = Invoke-RestMethod "$($relDef.url)?api-version=5.0" -Metho ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

最终网址:

http://138.202.18.216:8070/Samples/19e02b27-74ab-40dd-a519-ece38fafa870/_apis/build/Definitions/11?revision=5?api-version=5.0

最佳答案

您只需修复 url,将 api-version 之前的 ? 更改为 &:

?revision=5&api-version=5.0

revisionapi-version 是参数,如果你只有一个参数,你使用 但当你想使用更多时您需要将它们附加到 &

关于powershell - Azure DevOps 服务器调用 RestMethod 错误 : No API version provided for the "PUT" request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56956331/

相关文章:

powershell - 如何在 Powershell 中过滤掉两次文本?

powershell - Windows7 中的 Add-PsSnapin WebAdministration

tfs - 如何使用 TFS 数据创建自定义小部件?

azure-devops - Azure DevOps Rest Api 是否返回了正确数量的拉取请求?

powershell - AD cmdlet 和 powershell 命令有什么区别?

powershell - 为什么我的 Azure Powershell Runbook 在按计划执行时会挂起?如何防止重试?

azure - 在 Azure Web APP 部署任务中排除删除文件夹

c# - WebTestRequest.ReportingName 在使用 VS 团队服务时被忽略

tfs - 如何仅构建特定项目及其与 DevOps Pipelines 的依赖关系

rest - 如何使用 Azure DevOps REST API 创建新的构建管道?