AzureDevOps.VariableGroups/Public/Add-AzureDevOpsVariable.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function Add-AzureDevOpsVariable
{
    <#
    .SYNOPSIS
    Add or update a variable to a specified variable group
 
    .DESCRIPTION
    Adds or updates a variable in the specified variable group
 
    .PARAMETER GroupId
    The ID of the variable group to add/update the variable in.
 
    .PARAMETER Variable
    The variable object to add
 
    .EXAMPLE
    $myVariable = New-AzureDevOpsVariable -Name "MyVar" -Value "MyValue";
    Add-AzureDevOpsVariable -GroupId "2" -Variable $myVariable;
    #>

    param
    (
       [string]$GroupId = "",
       [PSCustomObject]$Variable
    )

    $group = Get-AzureDevOpsVariableGroup -GroupId $GroupId;
    if(Get-Member -InputObject $group.variables -Name $Variable.Name -MemberType NoteProperty)
    {
        $group.variables."$($Variable.Name)" = $Variable.Value;
    }
    else
    {
        Add-Member -InputObject $group.variables -MemberType NoteProperty -Name $Variable.Name -Value $Variable.Value;
    }

    $body = $group | ConvertTo-Json;
    return Invoke-AzureDevOpsRestMethod -PartialUri "/distributedtask/variablegroups/$($GroupId)?api-version=4.1-preview.1" -Method Put -Body $body
}