简体   繁体   中英

VSTS - Powershell script to add variable to release environment not working

I have a release definition in VSTS with an environment, and inside the environment I have a few variables, and I would like to be able to add from time to time one or another new variable but I need to do it automatically inside a process that creates a few things in other systems as well.

So I need to achieve this using powershell. I have the following script, it works without any error, the release definition is updated, but the variable is not inserted.

$ProjectUrl = "http://mytfsserver/tfs/myproject"
$DefinitionId = 7
$EnvironmentId = 11
$VariableName = "newvar"
$value = "newval"
$Comment = "Updated by PS script"

$ProjectUrl = $ProjectUrl.TrimEnd("/")

$url = "$($ProjectUrl)/_apis/release/definitions/$($DefinitionId)?expand=Environments?api-version=3.0-preview"
$definition = Invoke-RestMethod $url -UseDefaultCredentials

$environment = $definition.environments.Where{$_.id -eq $EnvironmentId}

if($environment)
{
    $environment.variables | Add-Member -Name $VariableName -MemberType NoteProperty -Value $value
}
else
{
    Write-Warning "Environment not found in the given release"
}

$definition.source = "restApi"

if ($Comment)
{
    $definition | Add-Member -Name "comment" -MemberType NoteProperty -Value $Comment
}

$body = $definition | ConvertTo-Json -Depth 100 -Compress

$body = [Text.Encoding]::UTF8.GetBytes($body)

$putUrl = "$($ProjectUrl)/_apis/release/definitions?api-version=3.0-preview"

Invoke-RestMethod $putUrl -Method Put -Body $body -ContentType 'application/json' -UseDefaultCredentials -Verbose -Debug

Can anyone help me understand what is missing or what is wrong?

Solved. $value was not being correctly formatter. It should be like this: $value = @{value=$value}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM