Public/New-AzDevOpsBlueprintAssignment.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function New-AzDevOpsBlueprintAssignment
{
    <#
    .SYNOPSIS
        Assigns a Blueprint in Azure

    .DESCRIPTION
        Assigns a version of a Blueprint that was published to a Management Group / Subscription

    .PARAMETER Blueprint
        An Object containing the Blueprint information.

    .PARAMETER AssignmentName
        A string containing the Assignment Name of the Blueprint

    .PARAMETER InputPath
        A string containing the path to the Blueprint JSON File e.g.
        'C:\Repos\Blueprints\Small_ISO27001_Shared-Services'

    .PARAMETER Location
        A string containing the azure region name to assign/create the resources for the Blueprint e.g. australiasoutheast

    .PARAMETER Version
        A string containing the Version of the Blueprint to be assigned. This value should be the Release Number generated by the Release pipeline e.g. 20190701.1 YYYYMMDD.R (YYYY - Year, MM = Month, DD = Day, R = Release number)

    .EXAMPLE Assign Blueprint to Subscription
        $Context = Get-AzContext
        $Blueprint = Get-AzBlueprint -Name "Assignment-Small_ISO27001_Shared-Services" -Subscription $Context.Subscription.Id
        New-AzDevOpsBlueprintAssignment `
            -Blueprint $Blueprint `
            -AssignmentName "Assignment-Small_ISO27001_Shared-Services" `
            -InputPath "C:\Repos\Blueprints\Small_ISO27001_Shared-Services" `
            -Location "australiasoutheast" `
            -Version "20190901.001"

    .EXAMPLE Assign Blueprint to Management Group
        $ManagementGroup = Get-AzManagementGroup | Where-Object DisplayName -eq "Development"
        $Blueprint = Get-AzBlueprintAssignment -Name "Assignment-Small_ISO27001_Shared-Services" -ManagementGroupId $ManagementGroup.Name
        New-AzDevOpsBlueprintAssignment `
            -Blueprint $Blueprint `
            -AssignmentName "Assignment-Small_ISO27001_Shared-Services" `
            -InputPath "C:\Repos\Blueprints\Small_ISO27001_Shared-Services" `
            -Location "australiasoutheast" `
            -Version "20190901.001"
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'None')]
    param
    (
        [parameter(Mandatory=$true)]
        $Blueprint,

        [parameter(Mandatory=$true)]
        [string]$AssignmentName,

        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [ValidateScript({Get-ChildItem -Path $_})]
        [string]$InputPath,

        [Parameter(Mandatory=$true)]
        [string]$Location,

        [Parameter(Mandatory=$true)]
        [string]$Version
    )

    function Confirm-Assignment
    {
        param
        (
            [parameter(Mandatory=$true)]
            $Blueprint,

            [parameter(Mandatory=$true)]
            $AssignmentName
        )

        Do
        {
            switch ($Blueprint)
            {
                {$Blueprint.SubscriptionId}
                {
                    $Assignment = Get-AzBlueprintAssignment -Name $AssignmentName -Subscription $Blueprint.SubscriptionId
                    break
                }

                {$Blueprint.ManagementGroupId}
                {
                    $Assignment = Get-AzBlueprintAssignment -Name $AssignmentName -ManagementGroupId $Blueprint.ManagementGroupId
                    break
                }
            }
            if ($Assignment.ProvisioningState) {    Write-Output $Assignment.ProvisioningState    }
            Start-Sleep -Seconds 5
        } Until (($Assignment.ProvisioningState -eq "Succeeded") -or ($Assignment.ProvisioningState -eq "Failed"))

        switch ($Assignment.ProvisioningState)
        {
            "Succeeded"
            {
                Write-Output "SUCCESS! Blueprint '$($Blueprint.Name) Version '$($Version)' has been Assigned"
                break
            }

            "Failed"
            {
                Write-Output "Error! Blueprint '$($Blueprint.Name) Version '$($Version)' failed"
                throw (Resolve-AzError -Last)
                break
            }

            default
            {
                throw "Unhandled terminal state for assignment: {0}" -f $assignment.ProvisioningState
            }
        }
    }

    try
    {
        # Checking if Commandlet is running on Azure DevOps
        if (!$env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI) {
            Write-Warning "It appears you are not running this command inside a job in Azure DevOps."
            Write-Warning "This command is designed to run as a job in Azure DevOps. See https://dev.azure.com/paulrtowler/Az.DevOps.Blueprint for more information."
            Write-Warning "Exiting...`r`n" -ErrorAction Stop
        }

        # Variables
        $rawBlueprint = Get-Content "$($InputPath)\Blueprint.json" | ConvertFrom-Json -ErrorAction Stop

        # Assign Blueprint
        Write-Output "`r`nAssigning Blueprint '$($Blueprint.Name)' using Version '$($Version)'....."
        Write-Output "Matching Variables to Blueprint Resource Groups and configuring Values....."

        $resourceGroups = Find-AzDevOpsBlueprintParameters -Type ResourceGroups -RawBlueprint $rawBlueprint -Location $Location

        if ($resourceGroups.count -ne 0)
        {
            Write-Output " Found Resource Group Parameters!"

            foreach ($resourceGroup in $resourceGroups)
            {
                Write-Output " Setting Resource Group Name to: $($resourceGroup.Values.Name)"
                Write-Output " Setting Resource location to: $($resourceGroup.Values.Location)`r`n"
            }
        }

        Write-Output "Matching Variables to Blueprint Parameters and configuring Values....."
        $params = Find-AzDevOpsBlueprintParameters -Type Parameters -RawBlueprint $rawBlueprint

        if ($params.count -ne 0)
        {
            Write-Output " Found Parameters!"

            foreach ($param in $params.Keys)
            {
                Write-Output " Setting Parameter: $($param)"
            }
        }

        Write-Output "Matching Variables to Blueprint Secure Parameters and configuring Values....."
        $secureParams = Find-AzDevOpsBlueprintParameters -Type SecureParameters -RawBlueprint $rawBlueprint

        if ($secureParams.count -ne 0)
        {
            Write-Output " Found Secure Parameters!"

            foreach ($param in $secureParams.Keys)
            {
                Write-Output " Setting Parameter: $($param)"
            }
        }

        switch ($Blueprint)
        {
            {$Blueprint.SubscriptionId}
            {
                # Get Any old Assignments
                $oldAssignment = Get-AzBlueprintAssignment -Name $AssignmentName -Subscription $Blueprint.SubscriptionId -ErrorAction SilentlyContinue

                if ($oldAssignment -and $PSCmdlet.ShouldProcess($AssignmentName, 'Proceed.')) {
                    # if yes, *update* the assignment
                    Write-Output "Updating existing assignment....."

                    if ($resourceGroups.Count -ge 1)
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -ErrorAction Stop
                        }
                    } else
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -Parameter $params -ErrorAction Stop
                        }
                    }
                } else
                {
                    # if no assignment, create one
                    Write-Output "Creating new assignment....."

                    if ($resourceGroups.Count -ge 1)
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -ErrorAction Stop
                        }
                    } else
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -SubscriptionId $Blueprint.SubscriptionId -Name $AssignmentName -Parameter $params -ErrorAction Stop
                        }
                    }
                }

                # Confirm Assignment
                Confirm-Assignment -Blueprint $Blueprint -AssignmentName $AssignmentName
                break
            }

            {$Blueprint.ManagementGroupId}
            {
                # Get Any old Assignments
                $oldAssignment = Get-AzBlueprintAssignment -Name $AssignmentName -ManagementGroupId $Blueprint.ManagementGroupId -ErrorAction SilentlyContinue

                if ($oldAssignment -and $PSCmdlet.ShouldProcess($AssignmentName, 'Proceed.')) {
                    # if yes, *update* the assignment
                    Write-Output "Updating existing assignment....."

                    if ($resourceGroups.Count -ge 1)
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -ErrorAction Stop
                        }
                    } else
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            Set-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -Parameter $params -ErrorAction Stop
                        }
                    }
                } else
                {
                    # if no assignment, create one
                    Write-Output "Creating new assignment....."

                    if ($resourceGroups.Count -ge 1)
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -ResourceGroupParameter $resourceGroups -Parameter $params -ErrorAction Stop
                        }
                    } else
                    {
                        if ($secureParams.Count -ge 1)
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -Parameter $params -SecureStringParameter $secureParams -ErrorAction Stop
                        } else
                        {
                            New-AzBlueprintAssignment -Blueprint $Blueprint -Location $Location -ManagementGroupId $Blueprint.ManagementGroupId -Name $AssignmentName -Parameter $params -ErrorAction Stop
                        }
                    }
                }

                # Confirm Assignment
                Confirm-Assignment -Blueprint $Blueprint -AssignmentName $AssignmentName
                break
            }
        }
    }
    catch
    {
        if ($_.ErrorDetails.Message) {$ErrDetails = $_.ErrorDetails.Message } else {$ErrDetails = $_}
        if ($_.Message) {$ErrDetails = $_.Message } else {$ErrDetails = $_}
        Get-StandardError -Exception $($ErrDetails)
    }
}