TeamFoundation/Public/Get-TfsReleaseStep.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function Get-TfsReleaseStep
{
    [CmdletBinding(DefaultParameterSetName = 'Tfs')]
    param
    (
        [Parameter(Mandatory)]
        [string]
        $InstanceName,

        [Parameter()]
        [string]
        $CollectionName = 'DefaultCollection',

        [ValidateRange(1, 65535)]
        [uint32]
        $Port,

        [Parameter(Mandatory, ParameterSetName = 'TfsName')]
        [Parameter(Mandatory, ParameterSetName = 'VstsName')]
        [SupportsWildcards()]
        [string]
        $FriendlyName,

        [Parameter(Mandatory, ParameterSetName = 'TfsHashtable')]
        [Parameter(Mandatory, ParameterSetName = 'VstsHashtable')]
        [hashtable]
        $FilterHashtable,

        [Parameter(Mandatory, ParameterSetName = 'TfsScript')]
        [Parameter(Mandatory, ParameterSetName = 'VstsScript')]
        [scriptblock]
        $FilterScript,

        [switch]
        $UseSsl,

        [Parameter(Mandatory, ParameterSetName = 'Tfs')]
        [Parameter(Mandatory, ParameterSetName = 'TfsName')]
        [Parameter(Mandatory, ParameterSetName = 'TfsHashtable')]
        [Parameter(Mandatory, ParameterSetName = 'TfsScript')]
        [pscredential]
        $Credential,
        
        [Parameter(Mandatory, ParameterSetName = 'Vsts')]
        [Parameter(Mandatory, ParameterSetName = 'VstsName')]
        [Parameter(Mandatory, ParameterSetName = 'VstsHashtable')]
        [Parameter(Mandatory, ParameterSetName = 'VstsScript')]
        [string]
        $PersonalAccessToken
    )

    $requestUrl = if ($UseSsl) {'https://' } else {'http://'}
    $requestUrl += if ( $Port -gt 0)
    {
        '{0}{1}/{2}/_apis/distributedtask/tasks' -f $InstanceName, ":$Port", $CollectionName
    }
    else
    {
        '{0}/{1}/_apis/distributedtask/tasks' -f $InstanceName, $CollectionName
    }

    $requestParameters = @{
        Uri         = $requestUrl
        Method      = 'Get'
        ErrorAction = 'Stop'
    }

    if ($Credential)
    {
        $requestParameters.Credential = $Credential
    }
    else
    {
        $requestParameters.Headers = @{ Authorization = Get-TfsAccessTokenString -PersonalAccessToken $PersonalAccessToken }
    }

    try
    {
        $result = Invoke-RestMethod @requestParameters
    }
    catch
    {
        Write-Error -ErrorRecord $_
    }
    
    $steps = if ($result.value)
    {
        $result.value | Where-Object -Property visibility -Contains 'Release'
    }
    elseif ($result)
    {
        $result | Where-Object -Property visibility -Contains 'Release'
    }

    if ($FriendlyName)
    {
        $steps = if ($FriendlyName -match '(\?|\*)')
        {
            $steps | Where-Object -Property friendlyName -like $FriendlyName
        }
        else
        {
            $steps | Where-Object -Property friendlyName -eq $FriendlyName
        }
    }

    if ($FilterHashtable)
    {
        $steps = foreach ( $kvp in $FilterHashtable.GetEnumerator())
        {
            if ($kvp.Value -match '(\?|\*)')
            {
                $steps | Where-Object -Property $kvp.Key -like $kvp.Value
            }
            else
            {
                $steps | Where-Object -Property $kvp.Key -eq $kvp.Value    
            }            
        }
    }

    if ($FilterScript)
    {
        $steps = $steps | Where-Object -FilterScript $FilterScript
    }

    '@('
    foreach ($step in $steps)
    {
        "
        @{
            enabled = `$true
            continueOnError = `$false
            alwaysRun = `$false
            timeoutInMinutes = 0
            definitionType = 'task'
            version = '*'
            name = 'YOUR OWN DISPLAY NAME HERE' # e.g. $($step.instanceNameFormat) or $($step.friendlyName)
            taskid = '$($step.id)'
            inputs = @{"

        foreach ($input in $step.inputs)
        {
            $required = if ($input.required) {$true}else {$false}
            "`t`t`t`t{0} = 'VALUE' # Type: {1}, Default: {2}, Mandatory: {3}" -f $input.name, $input.type, $input.defaultValue, $required
        }
        '
            }
        }
        '

    }
    ')'
}