I have an issue with a Powershell script to get, increment and put new value to a variable in a DevOps variablegroup.
There seem to be an issue with adding a variable in to the $json object string. Not sure how I can add the incremented value into the string, please advise!
The script:
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "","$env:SYSTEM_ACCESSTOKEN")))
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/1?api-version=6.0-preview.1"
$pipeline = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "Get Variable Value:" $pipeline.variables.MinorVersion.value
$versionNumber = [int]$pipeline.variables.MinorVersion.value
$versionNumber
Write-Host "$versionNumber Variable Value:" $versionNumber
$json = '{"id":2555,"type":"Vsts","name":"VersionNumberVG","variables":{"MinorVersion":{"value":$versionNumber}}}'
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "PUT New Variable Value:" $pipeline.variables.MinorVersion.value
$pipeline = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "Get Variable Value:" $pipeline.variables.MinorVersion.value
The error I get:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx.","typeName":"Newtonsoft.Jso
n.JsonReaderException, Newtonsoft.Json","typeKey":"JsonReaderException","errorCode":0,"eventId":0}
At line:13 char:13
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -Cont ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
CodePudding user response:
The cause of the issue is that the body format has issues.
You can try the following PowerShell sample to define the body and run the Rest API:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "","$env:SYSTEM_ACCESSTOKEN")))
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/1?api-version=6.0-preview.1"
$pipeline = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "Get Variable Value:" $pipeline.variables.MinorVersion.value
$versionNumber = [int]$pipeline.variables.MinorVersion.value
$versionNumber
Write-Host "$versionNumber Variable Value:" $versionNumber
$json =
"{
`"name`":`"VersionNumberVG`",
`"type`":`"Vsts`",
`"variables`":{
`"MinorVersion`":{
`"isSecret`":false,
`"value`": `"$versionNumber`"
}
}
}"
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "PUT New Variable Value:" $pipeline.variables.MinorVersion.value
$pipeline = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "Get Variable Value:" $pipeline.variables.MinorVersion.value
Result: