0

I'm using below code to update the Release definition in Powershell,

PowerShell_1:

Write-Host ">>>>>>>>>Start in Task 1: "$(pos)

Write-Host "##vso[task.setvariable variable=pos;]Yes"

PowerShell_2

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"

$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"


$pipeline.variables.pos.value = "$(pos)"


$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

Write-Host "After in Task 2: "$(pos)

But issue is, i'm using the above code in two Tasks in a Release Pipeline, The above code is passes in Task 1, but the same code throws below error on Task 2,

enter image description here

Release Variable: enter image description here

Please Help me to get out of this. Thanks in advance.

sanjai
  • 128
  • 1
  • 1
  • 13
  • Does this answer your question? [Update the Azure Release Variable value in PowerShell](https://stackoverflow.com/questions/64914402/update-the-azure-release-variable-value-in-powershell) – Repcak Nov 20 '20 at 10:40
  • it throws an below error, The property 'value' cannot be found on this object. Verify that the property exists and can be set. – sanjai Nov 20 '20 at 11:14
  • You didnt spend on the first question 5 minutes to research and didnt spend 5 minutes to do research on this one. Here is a guide: https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users – Repcak Nov 20 '20 at 11:45
  • I just copied your code and it passes without any issue. – Krzysztof Madej Nov 20 '20 at 12:53
  • Hi @sanjai, Just checking in to see whether this issue is still blocking you now? Any update for this issue? If yes, could you set the variable system.debug to true and then share the detail log here? We need check the log and help you local the issue. Thanks – Vito Liu Nov 24 '20 at 08:35
  • Hi @sanjai, Just checking in to see whether this issue is still blocking you now? Any update for this issue? – Vito Liu Nov 25 '20 at 08:39

3 Answers3

1

Something must have changed here 2021, reference to this developer community article. I am trying to update my Azure DevOps Pipeline Variable via PowerShell task in YAML. All authorizations are present. Here is my PowerShell task:

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

$pipeline.variables.test.value = "0"

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

write-host "==========================================================" 
Write-host "The value of Varialbe 'test' is updated to" $updatedef.variables.test.value
write-host "=========================================================="

Unfortunately I get the following error: The property 'value' cannot be found on this object. Verify that the property exists and can be set.

Any ideas?

I have found a solution for myself.

#Azure DevOps Personal Access Token
$MyPat = '*****';
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"));

#DevOps API
$url = 'https://dev.azure.com/<Organisation>/<Project>/_apis/distributedtask/variablegroups?api-version=6.0-preview.2';
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
Authorization = "Basic $B64Pat"
} 

#complete output as JSON
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)";

#my value in JSON
$variable = $pipeline.value.variables.MyVariable.value;
Brenners Daniel
  • 389
  • 5
  • 15
0

Because your answer in the $pipeline variable contains the source code of the error page.

enter image description here

I think, you use the wrong URL:

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"

You may try to use of the following for inline script:

$url = "$(System.TeamFoundationServerUri)/$(System.TeamProjectId)/_apis/Release/definitions/$(Release.DefinitionId)?api-version=5.0-preview.3"

or for PS file:

$serverUrl = $env:SYSTEM_TEAMFOUNDATIONSERVERURI
$teamProjectID = $env:SYSTEM_TEAMPROJECTID
$releaseID = $env:RELEASE_DEFINITIONID

$url = "$serverUrl/$teamProjectID/_apis/Release/definitions/$releaseID?api-version=5.0-preview.3"
Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • This does not solve the problem, I have tested it again. I think also the variable RELEASE_DEFINITIONID refers to the classical approach via releases - this one is empty for me. I use the procedure only via Pipelines. – Brenners Daniel Jan 28 '21 at 08:29
0

I have tested it with the code you shared and the details steps as below.

Create a new release definition->add variable pos and set the value to No

Add task power shell to set variable as env variable.

Write-Host ">>>>>>>>>Start in Task 1: "$(pos)

Write-Host "##vso[task.setvariable variable=pos;]Yes"

Result:

enter image description here

Add task bash and enter the script printenv to print all env variable to check it.

enter image description here

Add task power shell and enter below script to update the release variable.

$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"

$pipeline = Invoke-RestMethod -Uri $url -Headers @{
    Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"


$pipeline.variables.pos.value = $($env:POS)

Write-Host "The variable pos value is" $pipeline.variables.pos.value

$json = @($pipeline) | ConvertTo-Json -Depth 99


$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

Write-Host "After in Task 2: "$(pos)

Result:

enter image description here

Vito Liu
  • 7,525
  • 1
  • 8
  • 17