Powershell script is setting new release variable but it started failing suddenly
$url = "https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/$releaseId?api-version=5.0"
$releaseDefinition = Invoke-RestMethod -Uri $url -Method Get -Headers @{ "Authorization"="Bearer $authenticationToken" "Content-Type" = "application/json; charset=utf-8" }
$newVariable = New-Object PSObject
$newVariable | Add-Member Noteproperty allowOverride $False
$newVariable | Add-Member Noteproperty isSecret $secretValue
$newVariable | Add-Member Noteproperty value $variableValue
$serializedVariable = $newVariable | ConvertTo-Json -Depth 3
$deserializedVariable = $serializedVariable | ConvertFrom-Json
** 这是有问题的行。如果我注释掉这一行,则请求不会失败。当我记录releaseDefinition.variables 时,我看到添加了新变量。**
$releaseDefinition.variables | Add-Member -Name $variableName -Value $deserializedVariable - MemberType NoteProperty -Force
$requestBody = ConvertTo-Json $releaseDefinition -Depth 100
#Error throw here when we add new release variable
$response = Invoke-RestMethod -Uri $url -Method Put -Headers @{ "Authorization"="Bearer $authenticationToken" "Content-Type" = "application/json; charset=utf-8" } -Body $requestBody
错误信息: "Invoke-RestMethod : {"$id":"1","innerException":null,"message":"VS402865: POST 请求中不允许有空正文。""
针对错误
VS402865: An empty body in the POST request is not allowed.
,建议输出$requestBody
内容进行检查。
我尝试了你的脚本并修复了一些语法,它对我有用。我使用
system.accesstoken
作为 bearer token
并在 DevOps 版本中添加 powershell 任务:
$url = "https://vsrm.dev.azure.com/$(orgname)/$(projectname)/_apis/release/releases/$(Release.ReleaseId)?api-version=5.0"
$secretValue = "false"
$override = "false"
$variableValue = "var3value4"
$variableName = "var3"
$releaseDefinition = Invoke-RestMethod -Uri $url -Method Get -Headers @{"Authorization"="Bearer $env:SYSTEM_ACCESSTOKEN"; "Content-Type" = "application/json; charset=utf-8" }
$newVariable = New-Object PSObject
$newVariable | Add-Member Noteproperty allowOverride $override
$newVariable | Add-Member Noteproperty isSecret $secretValue
$newVariable | Add-Member Noteproperty value $variableValue
$serializedVariable = $newVariable | ConvertTo-Json -Depth 3
$deserializedVariable = $serializedVariable | ConvertFrom-Json
$releaseDefinition.variables | Add-Member -Name $variableName -Value $deserializedVariable -MemberType NoteProperty -Force
$requestBody = ConvertTo-Json $releaseDefinition -Depth 100
echo $requestBody
#Error throw here when we add new release variable
$response = Invoke-RestMethod -Uri $url -Method Put -Headers @{"Authorization"="Bearer $env:SYSTEM_ACCESSTOKEN"; "Content-Type" = "application/json; charset=utf-8" } -Body $requestBody
发布任务成功:
添加变量: