-
Notifications
You must be signed in to change notification settings - Fork 17
/
azure-az-patch-resource.ps1
536 lines (451 loc) · 20.4 KB
/
azure-az-patch-resource.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<#
.SYNOPSIS
powershell script to update (patch) existing azure arm template resource settings similar to resources.azure.com
.LINK
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/azure-az-patch-resource.ps1" -outFile "$pwd\azure-az-patch-resource.ps1";
.\azure-az-patch-resource.ps1 -resourceGroupName {{ resource group name }} -resourceName {{ resource name }} [-patch]
.DESCRIPTION
powershell script to update (patch) existing azure arm template resource settings similar to resources.azure.com.
useful for environments where resources.azure.com is not an option.
PRECAUTION: when script queries the arm resources, if unable to determine configured api version,
the latest api version for that resource will be written to the template.json.
.NOTES
File Name : azure-az-patch-resource.ps1
Author : jagilber
Version : 230806
History : add ordered
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup
enumerate all resources in resource group 'clusteresourcegroup' and generate template.json
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup -patch
patch all resources in resource group 'clusteresourcegroup' using existing template.json
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup -resource nt0
enumerate resource named 'nt0' in resource group 'clusteresourcegroup' and generate template.json
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup -resource nt0 -patch
patch all resource named 'nt0' in resource group 'clusteresourcegroup' using existing template.json
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup -templatejsonfile clusterresourcegroup.json
enumerate all resources in resource group 'clusteresourcegroup' and generate clusterresourcegroup.json
.EXAMPLE
.\azure-az-patch-resource.ps1 -resourceGroupName clusterresourcegroup -patch -templatejsonfile clusterresourcegroup.json
patch all resources in resource group 'clusteresourcegroup' using existing clusterresourcegroup.json
#>
[cmdletbinding()]
param (
[string]$resourceGroupName = '',
[string[]]$resourceNames = '',
[string[]]$excludeResourceNames = '',
[switch]$patch,
[string]$templateJsonFile = "$psscriptroot/template.json",
[string]$templateParameterFile = '',
[string]$apiVersion = '' ,
[string]$schema = 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json',
[int]$sleepSeconds = 1,
[ValidateSet('Incremental', 'Complete')]
[string]$mode = 'Incremental',
[switch]$useLatestApiVersion,
[switch]$pre,
[switch]$detail
)
set-strictMode -Version 3.0
$global:resourceTemplateObj = @{ }
$global:resourceErrors = 0
$global:resourceWarnings = 0
$global:configuredRGResources = [collections.arraylist]::new()
$env:SuppressAzurePowerShellBreakingChangeWarnings = $true
$PSModuleAutoLoadingPreference = 2
$currentErrorActionPreference = $ErrorActionPreference
$currentVerbosePreference = $VerbosePreference
$debugLevel = 'none'
function main () {
write-host "starting"
if ($detail) {
$ErrorActionPreference = 'continue'
$VerbosePreference = 'continue'
$debugLevel = 'all'
}
if (!(check-module)) {
return
}
if (!(@(Get-AzResourceGroup).Count)) {
write-host "connecting to azure"
Connect-AzAccount
}
if (!(Get-AzContext)) {
write-host "connecting to azure"
Connect-AzAccount
}
$global:startTime = get-date
if (!$resourceGroupName -or !$templateJsonFile) {
write-error 'specify resourceGroupName'
return
}
if ($resourceNames) {
foreach ($resourceName in $resourceNames) {
write-host "getting resource $resourceName"
$resource = @((get-azresource -ResourceGroupName $resourceGroupName -resourceName $resourceName))
if ($resource) {
$global:configuredRGResources.AddRange($resource)
}
else {
write-error "resource not found: $resourceName"
}
}
}
else {
write-host "getting resource group resource ids $resourceGroupName"
$resources = @((get-azresource -ResourceGroupName $resourceGroupName))
$global:configuredRGResources.AddRange($resources)
}
if ($excludeResourceNames) {
#$resources = $resources | where-object Name -NotMatch "$($excludeResourceNames -join "|")"
$global:configuredRGResources = $global:configuredRGResources.clone() | where-object Name -NotMatch "$($excludeResourceNames -join "|")"
}
display-settings -resources $global:configuredRGResources
if ($global:configuredRGResources.count -lt 1) {
Write-Warning "error enumerating resource $($error | format-list * | out-string)"
return
}
$deploymentName = "$resourceGroupName-$((get-date).ToString("yyyyMMdd-HHmms"))"
if ($patch) {
remove-jobs
if (!(deploy-template)) { return }
wait-jobs
}
else {
export-template -configuredResources $global:configuredRGResources
write-host "template exported to $templateJsonFile" -ForegroundColor Yellow
write-host "to update arm resource, modify $templateJsonFile. when finished, execute script with -patch to update resource" -ForegroundColor Yellow
$error.clear()
code $templateJsonFile
if ($error) {
. $templateJsonFile
}
}
if ($global:resourceErrors -or $global:resourceWarnings) {
write-warning "deployment may not have been successful: errors: $global:resourceErrors warnings: $global:resourceWarnings"
if ($DebugPreference -ieq 'continue') {
write-host "errors: $($error | sort-object -Descending | out-string)"
}
}
write-host "Get-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name $deploymentName -ErrorAction silentlycontinue"
$deployment = Get-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -Name $deploymentName -ErrorAction silentlycontinue
if ($deployment) {
write-host "deployment:`r`n$($deployment | format-list * | out-string)"
}
Write-Progress -Completed -Activity "complete"
write-host "time elapsed: $(((get-date) - $global:startTime).TotalMinutes.ToString("0.0")) minutes`r`n"
write-host 'finished. template stored in $global:resourceTemplateObj' -ForegroundColor Cyan
}
function check-module() {
$error.clear()
get-command Connect-AzAccount -ErrorAction SilentlyContinue
if ($error) {
$error.clear()
write-warning "azure module for Connect-AzAccount not installed."
if ((read-host "is it ok to install latest azure az module?[y|n]") -imatch "y") {
$error.clear()
install-module az.accounts
install-module az.resources
import-module az.accounts
import-module az.resources
}
else {
return $false
}
if ($error) {
return $false
}
}
return $true
}
function create-jsonTemplate([collections.arraylist]$resources,
[string]$jsonFile,
[hashtable]$parameters = @{},
[hashtable]$variables = @{},
[hashtable]$outputs = @{}
) {
try {
$resourceTemplate = [ordered]@{
'$schema' = $schema
contentVersion = "1.0.0.0"
parameters = $parameters
variables = $variables
resources = $resources
outputs = $outputs
} | convertto-json -depth 99
$resourceTemplate | out-file $jsonFile
write-host $resourceTemplate -ForegroundColor Cyan
$global:resourceTemplateObj = $resourceTemplate | convertfrom-json
return $true
}
catch {
write-error "$($_)`r`n$($error | out-string)"
return $false
}
}
function deploy-template() {
$templateParameters = @{}
$parameters = @{}
$variables = @{}
$outputs = @{}
$json = get-content -raw $templateJsonFile | convertfrom-json
if (!$json -or !$json.resources) {
write-error "invalid template file $templateJsonFile"
return
}
if ((test-path $templateParameterFile)) {
$jsonParameters = get-content -raw $templateParameterFile | convertfrom-json
# convert pscustom object to hashtable
foreach ($jsonParameter in $jsonParameters.parameters.psobject.properties) {
$templateParameters.Add($jsonParameter.name, $jsonParameter.value.value)
}
}
$templateJsonFile = Resolve-Path $templateJsonFile
$tempJsonFile = "$([io.path]::GetDirectoryName($templateJsonFile))\$([io.path]::GetFileNameWithoutExtension($templateJsonFile)).temp.json"
$resources = @($json.resources)
foreach ($jsonParameter in $json.parameters.psobject.properties) {
$parameters.Add($jsonParameter.name, $jsonParameter.value)
}
foreach ($jsonParameter in $json.variables.psobject.properties) {
$variables.Add($jsonParameter.name, $jsonParameter.value)
}
foreach ($jsonParameter in $json.outputs.psobject.properties) {
$outputs.Add($jsonParameter.name, $jsonParameter.value)
}
# remove provisioningstate as it is not a valid property for deploy and will cause validation error if state is not equal to current state
foreach ($resource in $resources) {
$resourceProperties = $resource.properties.psobject.properties
foreach ($resourceProperty in $resourceProperties) {
if ($resourceProperty.Name -ieq 'provisioningState') {
$resourceProperty.Value = $null
write-host "removing provisioningState from resource: $($resource.id)" -ForegroundColor Yellow
continue
}
}
}
create-jsonTemplate -resources $resources -jsonFile $tempJsonFile -parameters $parameters -variables $variables -outputs $outputs | Out-Null
$error.Clear()
write-host "validating template: Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName ``
-TemplateFile $tempJsonFile ``
-Verbose:$detail ``
-TemplateParameterObject $templateParameters ``
-Debug:$detail ``
-Mode $mode" -ForegroundColor Cyan
$result = Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
-TemplateFile $tempJsonFile `
-Verbose:$detail `
-TemplateParameterObject $templateParameters `
-Debug:$detail `
-Mode $mode
if (!$error -and !$result) {
write-host "patching resource with $tempJsonFile" -ForegroundColor Yellow
start-job -ScriptBlock {
param ($resourceGroupName, $tempJsonFile, $deploymentName, $mode, $debugLevel, $detail, $templateParameters)
if ($detail) {
$VerbosePreference = 'continue'
}
write-host "using file: $tempJsonFile"
write-host "deploying template: New-AzResourceGroupDeployment -Name $deploymentName ``
-ResourceGroupName $resourceGroupName ``
-DeploymentDebugLogLevel $debuglevel ``
-TemplateFile $tempJsonFile ``
-TemplateParameterObject $templateParameters ``
-Verbose:$detail ``
-Mode $mode" -ForegroundColor Cyan
New-AzResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $resourceGroupName `
-DeploymentDebugLogLevel $debugLevel `
-TemplateFile $tempJsonFile `
-TemplateParameterObject $templateParameters `
-Verbose:$detail `
-Mode $mode
} -ArgumentList $resourceGroupName, $tempJsonFile, $deploymentName, $mode, $debugLevel, $detail, $templateParameters
}
else {
write-host "template validation failed: $($error |out-string) $($result | out-string)"
write-error "template validation failed`r`n$($error | convertto-json)`r`n$($result | convertto-json)"
return $false
}
if ($error) {
return $false
}
else {
return $true
}
}
function display-settings($resources) {
$settings = @()
foreach ($resource in $resources) {
$settings += $resource | convertto-json -depth 99
}
write-host "current settings: `r`n $settings" -ForegroundColor Green
}
function export-template($configuredResources) {
write-host "exporting template to $templateJsonFile" -ForegroundColor Yellow
$resources = [collections.arraylist]@()
$azResourceGroupLocation = @($configuredResources)[0].Location
write-host "getting latest api versions" -ForegroundColor yellow
$resourceProviders = Get-AzResourceProvider -Location $azResourceGroupLocation -Pre:$pre
write-host "getting configured api versions" -ForegroundColor green
Export-AzResourceGroup -ResourceGroupName $resourceGroupName -Path $templateJsonFile -Force
$currentConfig = Get-Content -raw $templateJsonFile | convertfrom-json
$currentApiVersions = $currentConfig.resources | select-object type, apiversion | sort-object -Unique type
write-host ($currentApiVersions | format-list * | out-string)
remove-item $templateJsonFile -Force
foreach ($azResource in $configuredResources) {
write-verbose "azresource by id: $($azResource | format-list * | out-string)"
$resourceApiVersion = $null
if (!$useLatestApiVersion -and $currentApiVersions.type.contains($azResource.ResourceType)) {
$rpType = $currentApiVersions | where-object type -ieq $azResource.ResourceType | select-object apiversion
$resourceApiVersion = $rpType.ApiVersion
write-host "using configured resource schema api version: $resourceApiVersion to enumerate and save resource: `r`n`t$($azResource.ResourceId)" -ForegroundColor green
}
if ($useLatestApiVersion -or !$resourceApiVersion) {
$resourceProvider = $resourceProviders | where-object ProviderNamespace -ieq $azResource.ResourceType.split('/')[0]
$rpType = $resourceProvider.ResourceTypes | where-object ResourceTypeName -ieq $azResource.ResourceType.split('/')[1]
$resourceApiVersion = $rpType.ApiVersions[0]
write-host "using latest schema api version: $resourceApiVersion to enumerate and save resource: `r`n`t$($azResource.ResourceId)" -ForegroundColor yellow
}
$azResource = get-azresource -Id $azResource.ResourceId -ExpandProperties -ApiVersion $resourceApiVersion
write-verbose "azresource by id and version: $($azResource | format-list * | out-string)"
$resource = @{
apiVersion = $resourceApiVersion
#dependsOn = @()
type = $azResource.ResourceType
location = $azResource.Location
id = $azResource.ResourceId
name = $azResource.Name
tags = $azResource.Tags
properties = $azResource.properties
}
# add other arm objects. vmss sku is an example
foreach ($item in $azResource.psobject.properties) {
write-verbose "searching psobject for resourcemanager objects: item: $item"
if ($item.TypeNameOfValue -imatch 'Microsoft.Azure.Management.ResourceManager') {
write-verbose "adding psobject for resourcemanager objects: item: $item"
[void]$resource.Add($item.Name, $azresource."$($item.Name)")
}
}
[void]$resources.Add($resource)
}
if (!(create-jsonTemplate -resources $resources -jsonFile $templateJsonFile)) { return }
return
}
function remove-jobs() {
try {
foreach ($job in get-job) {
write-host "removing job $($job.Name)"
write-host $job
$job.StopJob()
Remove-Job $job -Force
}
}
catch {
write-host "error:$($Error | out-string)"
$error.Clear()
}
}
function wait-jobs() {
write-log "monitoring jobs"
while (get-job) {
foreach ($job in get-job) {
$jobInfo = (receive-job -Id $job.id)
if ($jobInfo) {
write-log -data $jobInfo
}
else {
write-log -data $job
}
if ($job.state -ine "running") {
write-log -data $job
if ($job.state -imatch "fail" -or $job.statusmessage -imatch "fail") {
write-log -data $job
}
write-log -data $job
remove-job -Id $job.Id -Force
}
write-progressInfo
start-sleep -Seconds $sleepSeconds
}
}
write-log "finished jobs"
}
function write-log($data) {
if (!$data) { return }
[text.stringbuilder]$stringData = New-Object text.stringbuilder
if ($data.GetType().Name -eq "PSRemotingJob") {
foreach ($job in $data.childjobs) {
if ($job.Information) {
$stringData.appendline(@($job.Information.ReadAll()) -join "`r`n")
}
if ($job.Verbose) {
$stringData.appendline(@($job.Verbose.ReadAll()) -join "`r`n")
}
if ($job.Debug) {
$stringData.appendline(@($job.Debug.ReadAll()) -join "`r`n")
}
if ($job.Output) {
$stringData.appendline(@($job.Output.ReadAll()) -join "`r`n")
}
if ($job.Warning) {
write-warning (@($job.Warning.ReadAll()) -join "`r`n")
$stringData.appendline(@($job.Warning.ReadAll()) -join "`r`n")
$stringData.appendline(($job | format-list * | out-string))
$global:resourceWarnings++
}
if ($job.Error) {
write-error (@($job.Error.ReadAll()) -join "`r`n")
$stringData.appendline(@($job.Error.ReadAll()) -join "`r`n")
$stringData.appendline(($job | format-list * | out-string))
$global:resourceErrors++
}
if ($stringData.tostring().Trim().Length -lt 1) {
return
}
}
}
else {
$stringData = "$(get-date):$($data | format-list * | out-string)"
}
write-host $stringData
}
function write-progressInfo() {
$ErrorActionPreference = $VerbosePreference = 'silentlycontinue'
$errorCount = $error.Count
write-verbose "Get-AzResourceGroupDeploymentOperation -ResourceGroupName $resourceGroupName -DeploymentName $deploymentName -ErrorAction silentlycontinue"
$deploymentOperations = Get-AzResourceGroupDeploymentOperation -ResourceGroupName $resourceGroupName -DeploymentName $deploymentName -ErrorAction silentlycontinue
$status = "time elapsed: $(((get-date) - $global:startTime).TotalMinutes.ToString("0.0")) minutes" #`r`n"
write-verbose $status
$pattern = "/subscriptions/(?<subscriptionId>.+?)/resourceGroups/(?<resourceGroup>.+?)/providers/(?<provider>.+?)/(?<providerType>.+?)/(?<resource>.+)"
$count = 0
Write-Progress -Activity "deployment: $deploymentName resource patching: $resourceGroupName" -Status $status -id ($count++)
if ($deploymentOperations) {
write-verbose ("deployment operations: `r`n`t$($deploymentOperations | out-string)")
foreach ($operation in $deploymentOperations) {
write-verbose ($operation | convertto-json)
$result = [regex]::Match($operation.targetResource, $pattern, [text.regularexpressions.regexoptions]::IgnoreCase);
$providerType = $result.Groups['providerType'].Value
$resource = $result.Groups['resource'].Value
$currentOperation = "$($providerType):$($resource)"
$status = "$($operation.provisioningState):$($operation.statusCode)"
if ($operation.statusMessage) {
$status = "$status $($operation.statusMessage)"
}
Write-Progress -Activity $currentOperation -id ($count++) -Status $status
}
}
if ($errorCount -ne $error.Count) {
$error.RemoveRange($errorCount - 1, $error.Count - $errorCount)
}
if ($detail) {
$ErrorActionPreference = $VerbosePreference = 'continue'
}
}
main
$ErrorActionPreference = $currentErrorActionPreference
$VerbosePreference = $currentVerbosePreference