AzureDevOps.VariableGroups/Public/Copy-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
function Copy-AzureDevOpsVariableGroup
{
    <#
    .SYNOPSIS
    Copy a variable group by ID
 
    .DESCRIPTION
    Creates a copy of the specified variable group
 
    .PARAMETER GroupId
    The variable group ID to copy
 
    .PARAMETER GroupName
    The source variable group name.
 
    .PARAMETER NewGroupName
    The name for the newly created variable group. This will default to the source name with a (Copy) suffix.
 
    .EXAMPLE
    Copy-AzureDevOpsVariableGroup -GroupId "2"
    #>

    param
    (
        [Parameter(
            ParameterSetName='GroupId',
            Mandatory=$true
        )]
        [string]$GroupId,

        [Parameter(
            ParameterSetName='GroupName',
            Mandatory=$true
        )]
        [string]$GroupName,
        [string]$NewGroupName
    )

    if($GroupId)
    {
        $group = Get-AzureDevOpsVariableGroup -GroupId $GroupId;
    }
    elseif($GroupName)
    {
        $group = (Get-AzureDevOpsVariableGroup -SearchString $GroupName)[0];
    }

    if(-not $NewGroupName)
    {
        $NewGroupName = "$($group.name) (Copy)";
    }

    # Create a new group object
    $newGroup = New-AzureDevOpsVariableGroup -Name $newGroupName -Description $group.description -Variables @();
    
    # Set variables and provider data
    $newGroup.variables = $group.variables;
    $newGroup | Add-Member -MemberType NoteProperty -Name providerData -Value $group.providerData;

    # Create the new group
    return Add-AzureDevOpsVariableGroup -Group $newGroup;
}