AzureDevOps.VariableGroups/Public/Get-AzureDevOpsVariableGroup.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Get-AzureDevOpsVariableGroup
{
    <#
    .SYNOPSIS
    Retrieve a variable group or a set of variable groups.
 
    .DESCRIPTION
    Retrieves a variable group by its ID or a set of variable groups based on a search string.
 
    .PARAMETER GroupId
    The variable group ID. If this parameter is specified. the variable group with the specified ID will be retrieved.
 
    .PARAMETER SearchString
    The search string. If this parameter is specified, the variable groups whose names start with the specified search string will be retrieved.
 
    .EXAMPLE
    Get-AzureDevOpsVariableGroup -GroupId "2"
 
    Gets the variable group with ID: "2".
 
    .EXAMPLE
    Get-AzureDevOpsVariableGroup -SearchString "contoso"
 
    Gets variable groups which have names starting with "contoso".
    #>

    [CmdletBinding(DefaultParameterSetName = 'GroupId')]
    param
    (
       [Parameter(ParameterSetName='GroupId')]
       [string]$GroupId = "",

       [Parameter(ParameterSetName='SearchString')]
       [string]$SearchString = ""
    )

    if($PSCmdlet.ParameterSetName -eq 'GroupId')
    {
        $result = Invoke-AzureDevOpsRestMethod -PartialUri "/distributedtask/variablegroups/$($GroupId)?api-version=4.1-preview.1" -Method Get;
        
        if($null -eq $result)
        {
            Write-Warning -Message "No variable groups found with ID '$GroupId'.";
        }

        return $result
    }
    else
    {
        if($SearchString)
        {
            $PartialUri = "/distributedtask/variablegroups/?groupName=$SearchString*&api-version=4.1-preview.1";
        }
        else
        {
            $PartialUri = "/distributedtask/variablegroups/?api-version=4.1-preview.1";
        }

        $result = Invoke-AzureDevOpsRestMethod -PartialUri $PartialUri -Method Get;
        
        if($result.count -eq 0)
        {
            Write-Warning -Message "No variable groups found with a name that starts with '$SearchString'.";
        }
        
        return $result.value;
    }
}