AdoAzHelper.psm1

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Connect-AahServiceConnection
{
    [CmdletBinding()]
    param
    (
        #ServiceConnection
        [Parameter(Mandatory=$true)]
        [psobject]$ServiceConnectionId,

        #PassThru
        [Parameter(Mandatory=$false)]
        [switch]$PassThru
    )

    process
    {
        #Get ServiceConnection
        try
        {
            Write-Information "Get ServiceConnection started"

            $ServiceConnection = Get-VstsEndpoint -Name $ServiceConnectionId -Require

            Write-Information "Get ServiceConnection completed"
        }
        catch
        {
            throw "Get ServiceConnection failed. Details: $_"
        }

        #Connect using ServiceConnection
        try
        {
            Write-Information "Connect using ServiceConnection started"

            switch ($ServiceConnection.Auth.scheme)
            {
                'ServicePrincipal' {
                    
                    switch ($ServiceConnection.Auth.Parameters.AuthenticationType)
                    {
                        'spnKey' {

                            Write-Information "Connect using ServiceConnection in progress. Connecting to subscription:'$($ServiceConnection.Data.subscriptionName)' using 'ServicePrincipal with spnKey'"
                            $ConnectAzAccount_Params = @{
                                ContextName="aah_$ServiceConnectionId"
                                ServicePrincipal=$true
                                Tenant=$ServiceConnection.Auth.Parameters.TenantId
                                Subscription=$ServiceConnection.Data.subscriptionId
                                Credential=[System.Management.Automation.PSCredential]::new($ServiceConnection.Auth.Parameters.ServicePrincipalId,(ConvertTo-SecureString $ServiceConnection.Auth.Parameters.ServicePrincipalKey -AsPlainText -Force))
                                SkipContextPopulation=$true
                                Scope='Process'
                            }
                            $profile = Connect-AzAccount @ConnectAzAccount_Params -ErrorAction Stop
                            if ($PassThru.IsPresent)
                            {
                                $profile.context
                            }
                        }

                        default { throw "Unsupported ServiceConnection AuthenticationType: $_" }
                    }

                    break
                }


                default { throw "Unsupported ServiceConnection Auth Schema: $_" }
            }

            Write-Information "Connect using ServiceConnection completed"
        }
        catch
        {
            throw "Connect using ServiceConnection failed. Details: $_"
        }
    }
}

function Disconnect-AahServiceConnection
{
    [CmdletBinding()]
    param
    (
        #ServiceConnection
        [Parameter(Mandatory=$true,ParameterSetName='Specific')]
        [psobject[]]$ServiceConnectionId,

        #All
        [Parameter(Mandatory=$true,ParameterSetName='All')]
        [switch]$All
    )

    process
    {
        #Get ServiceConnections in Scope
        try
        {
            Write-Information "Get ServiceConnections in Scope started"

            $ServiceConnectionsInScope = [System.Collections.Generic.List[string]]::New()

            switch($PSCmdlet.ParameterSetName)
            {
                'Specific' {
                    $ContextNamesInScope = $ServiceConnectionId | foreach {"aah_$_"}
                    Get-AzContext -ListAvailable | Where-Object {$_.Name -in $ContextNamesInScope} | foreach {
                        $ServiceConnectionsInScope.Add($_.Name)
                    }
                    break
                }
    
                'All' {
                    $ServiceConnections = Get-AzContext -ListAvailable | Where-Object {$_.Name -like 'aah_*'} | foreach {
                        $ServiceConnectionsInScope.Add($_.Name)
                    }
                    break
                }
    
                default { throw "Unsupported ParameterSetName: $_" }
            }

            Write-Information "Get ServiceConnections in Scope completed"
        }
        catch
        {
            throw "Get ServiceConnections in Scope failed. Details: $_"
        }

        #Disconnect ServiceConnections
        try
        {
            Write-Information "Disconnect ServiceConnections started"

            if ($ServiceConnectionsInScope.Count -gt 0)
            {
                $ServiceConnectionsInScope | foreach {
                    try
                    {
                        Write-Information "Disconnect ServiceConnections in progress. Disconnecting: $_"
                        $null = Disconnect-AzAccount -ContextName $_ -ErrorAction Stop
                    }
                    catch
                    {
                        Write-Warning "Disconnect ServiceConnections in progress. Failed to disconnect: $_"
                    }
                }

                Write-Information "Disconnect ServiceConnections completed"
            }
            else
            {
                Write-Information "Disconnect ServiceConnections skipped. No ServiceConnections in scope"
            }
        }
        catch
        {
            throw "Disconnect ServiceConnections failed. Details: $_"
        }
    }
}

function Set-AahPipelineVariable
{
    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory=$true)]
        [AahPipelineVariable[]]$InputObject
    )

    process
    {
        foreach ($object in $InputObject) 
        {
            "##vso[task.setvariable variable=$($object.Name);isSecret=$($object.isSecret);isOutput=$($object.isOutput)]$($Object.Value)"
        }
    }
}

#region classes

class AahPipelineVariable
{
    [string]$Name
    [string]$Value
    [bool]$isSecret
    [bool]$isOutput
}

#endregion