VSTS-Powershell脚本添加变量以释放环境不起作用

时间:2018-08-23 16:53:04

标签: powershell azure-pipelines-release-pipeline

我在带有环境的VSTS中有一个发布定义,并且在环境中我有一些变量,我希望能够不时添加一个或另一个新变量,但我需要在内部自动进行在其他系统中也会创建一些东西的过程。

所以我需要使用powershell来实现。 我有以下脚本,它可以正常运行,发布定义已更新,但未插入变量。

$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

任何人都可以帮助我了解缺失或错误的地方吗?

1 个答案:

答案 0 :(得分:0)

已解决。 $ value格式不正确。应该是这样的:$ value = @ {value = $ value}