-
Notifications
You must be signed in to change notification settings - Fork 2
/
ADF-BCDR.ps1
752 lines (657 loc) · 36.7 KB
/
ADF-BCDR.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
function Write-OutLog($msg, $foregroundcolor = "white") {
Write-Host $msg -ForegroundColor $foregroundcolor
"$(get-date): $msg" | Out-File -FilePath "$($pwd.Path)\adfbcdr.log" -Append
}
function Invoke-AzCmd {
[CmdletBinding()]
param (
[Parameter()]
[string]
$cmd,
[bool]
$deserialize = $true
)
Write-OutLog "Command Running: $cmd"
$scriptblock = { $cmd }
$result = Invoke-Expression $cmd 2>&1
if ($LASTEXITCODE -ne 0) {
#get the stderr of invoke-expression, log it.
Write-OutLog "Last exit code was $LASTEXITCODE" -ForegroundColor Red
Write-OutLog $Error[1] -ForegroundColor Red
Write-OutLog $Error[0] -ForegroundColor Red
throw $Error[0]
}
<#Invoke-AzCmd deserialize=$false flag is intended for pulling ARM templates.
Therefore, we will only check for continuation token where we're sending back objects#>
switch ($deserialize) {
$true {
if ((($result | convertfrom-json ) | get-member -name nextLink) -ne $null) {
$results += ($result | convertfrom-json ).value
$nextlink = ($result | convertfrom-json ).nextLink
$bDone = $false
while ($bDone -eq $false) {
Write-OutLog "trying nextlink: $nextlink" -ForegroundColor Yellow
$tmp = (Invoke-AzCmd "az rest --uri `'`"$nextlink`"`' --method get")
$results += $tmp
if ($tmp.nextLink -eq $null)
{ $bDone = $true }
else
{ $nextlink = $tmp.nextLink }
}
}
else {
$results += ($result | convertfrom-json )
}
if (($results | get-member -name value) -ne $null) {
Write-OutLog "returning from paged result with value" -foregroundcolor red
return $results.value
}
else {
Write-OutLog "returning from paged result without value" -foregroundcolor red
return $results
}
}
$false {
return $result
}
}
}
function backup-adffactory($sub, $rg, $adf, $outputfile) {
Write-OutLog "Starting backup of factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$($adf)?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function deploy-adffactory($sub, $rg, $adf, $inputfile, $region = "") {
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$($adf)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile | convertfrom-json)
$template.name = $adf
#This is required because if you are creating from scratch, it needs to be blank (creates a new MSI)
#TODO: Check and see if already exists. This is not desirable when it already exists.
if ($template.identity.type -eq "SystemAssigned") {
$template.identity.principalId = $null
$template.identity.tenantId = $null
}
#restore to different region
if ($region -ne "") {
$template.location = $region
}
#This bit enables cross-subscription restore
$template.id = "/subscriptions/$sub/resourceGroups/$rg/providers/Microsoft.DataFactory/factories/$adf$($suffix)"
$body = $template | convertto-json
$headers = @{}
$headers["Authorization"] = "Bearer $token"
$headers["Content-Type"] = "application/json"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers
}
function backup-adflinkedservice($sub, $rg, $adf, $linkedservice, $outputfile) {
Write-OutLog "Starting backup of linked service $linkedservice in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/linkedservices/$($linkedservice)?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function deploy-adflinkedservice($sub, $rg, $adf, $linkedservice, $inputfile) {
$linkedservice = $linkedservice.Replace(" ", "_")
Write-OutLog "Starting restore of linked service $linkedservice in factory $adf in resource group $rg"
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/linkedservices/$($linkedservice)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile | convertfrom-json)
$template.name = $linkedservice
$body = $template | convertto-json -depth 10
$headers = @{}
$headers["Authorization"] = "Bearer $token"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers
}
function backup-adfintegrationruntime($sub, $rg, $adf, $integrationruntime, $outputfile) {
Write-OutLog "Starting backup of linked service $linkedservice in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/integrationruntimes/$($integrationruntime)?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function deploy-adfintegrationruntime($sub, $rg, $adf, $integrationruntime, $inputfile) {
Write-OutLog "Starting restore of linked service $linkedservice in factory $adf in resource group $rg"
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/integrationruntimes/$($integrationruntime)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile)
$body = $template | convertto-json -depth 4
$headers = @{}
$headers["Authorization"] = "Bearer $token"
$headers["content-type"] = "application/json"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers
}
function backup-adfdataflow($sub, $rg, $adf, $dataflow, $outputfile) {
Write-OutLog "Starting backup of data flow $dataflow in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/dataFlows/$($dataflow)?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function deploy-adfdataflow($sub, $rg, $adf, $dataflow, $inputfile, $folder = $null) {
$dataflow = $dataflow.Replace(" ", "_")
Write-OutLog "Starting restore of data flow $dataflow in factory $adf in resource group $rg"
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/dataFlows/$($dataflow)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile | convertfrom-json)
$template.name = $dataflow
<#
if (($null -eq $folder) -or ($folder -eq "")) {
if ($template.properties.folder -ne $null) {
$template.properties.folder.name = $folder
}
else {
$template.properties | add-member -Name "folder" -Value ("{ `"name`": `"$folder`" }" | convertfrom-json) -MemberType NoteProperty
}
}
#>
$body = $template | convertto-json -Depth 4
$headers = @{}
$headers["Authorization"] = "Bearer $token"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers
}
function backup-adfdataset($sub, $rg, $adf, $dataset, $outputfile) {
Write-OutLog "Starting backup of dataset $dataset in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/datasets/$($dataset)?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function deploy-adfdataset($sub, $rg, $adf, $dataset, $inputfile, $folder = $null) {
Write-OutLog "Starting deploy of data set $dataset in factory $adf in resource group $rg"
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/datasets/$($dataset)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = get-content -Path $inputfile | convertfrom-json
<#
if (($null -eq $folder) -or ($folder -eq "")) {
if ($template.properties.folder -ne $null) {
$template.properties.folder.name = $folder
}
else {
$template.properties | add-member -Name "folder" -Value ("{ `"name`": `"$folder`" }" | convertfrom-json) -MemberType NoteProperty
}
$body = $template | convertto-json -Depth 4
}
else {
$body = get-content -Path $inputfile
}
#>
$body = get-content -Path $inputfile
$headers = @{}
$headers["Authorization"] = "Bearer $token"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers
}
function backup-adfpipeline($sub, $rg, $adf, $pipeline, $outputfile) {
Write-OutLog "Starting backup of pipeline $pipeline in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/pipelines/$($pipeline)?api-version=2018-06-01`'"
#"`'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/pipelines/{pipelineName}?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
<# We can use this verbatim - no fixup needed.
There's some extra stuff in the JSON, like ETAG, modified time, etc. But this gets stripped when you publish it.
Meanwhile, it's data which is useful in source control as forensic/historical info.#>
$json | out-file $outputfile
}
function Deploy-AdfPipeline {
[CmdletBinding()]
param (
$sub,
$rg,
$adf,
$pipeline,
$inputfile,
$folder = $null
)
Write-OutLog "Starting restore of pipeline $pipeline in factory $adf in resource group $rg"
$pipeline = $pipeline.Replace(" ", "_") # GAP is using underscores in their pipeline names
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/pipelines/$($pipeline)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile | convertfrom-json)
$body = $template
$headers = @{}
$headers["Authorization"] = "Bearer $token"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
try {
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers -Verbose
}
catch {
$message = $_.Exception
Write-OutLog $message -ForegroundColor Red
throw "$message"
}
}
function Backup-AdfTrigger {
[CmdletBinding()]
param (
$sub,
$rg,
$adf,
$trigger,
$outputfile
)
Write-OutLog "Starting backup of trigger $trigger in factory $adf in resource group $rg"
# Get the pipeline via AZ CLI - gives us the cleanest JSON
$uri = "`'https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/triggers/$($trigger)?api-version=2018-06-01`'"
#"`'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/pipelines/{pipelineName}?api-version=2018-06-01`'"
$json = Invoke-AzCmd "az rest --uri $uri --method get" -deserialize $false
$json | out-file $outputfile
}
function Deploy-AdfTrigger {
[CmdletBinding()]
param (
$sub,
$rg,
$adf,
$trigger,
$inputfile,
$folder = $null
)
Write-OutLog "Starting restore of trigger $trigger in factory $adf in resource group $rg"
$pipeline = $pipeline.Replace(" ", "_")
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$rg/providers/Microsoft.DataFactory/factories/$adf/triggers/$($trigger)?api-version=2018-06-01"
$token = (Invoke-AzCmd "az account get-access-token").accessToken
$template = (get-content -Path $inputfile | convertfrom-json)
$template.name = $pipeline
if ($null -ne $folder) {
if ($null -ne $template.properties.folder ) {
$template.properties.folder.name = $folder
}
else {
$template.properties | add-member -Name "folder" -Value ("{ `"name`": `"$folder`" }" | convertfrom-json) -MemberType NoteProperty
}
}
$body = $template | convertto-json -Depth 4
$headers = @{}
$headers["Authorization"] = "Bearer $token"
Write-OutLog "Callling REST method: $uri"
#Using REST call direct, as AZ CLI has some validation which the pipeline JSON doesn't pass for some reason.
try {
Invoke-RestMethod -Method Put -Uri $uri -body $body -Headers $headers -Verbose
}
catch {
$message = $_.Exception
Write-OutLog $message -ForegroundColor Red
throw "$message"
}
}
function ensure-adfdirectory($srcpath) {
if (Test-Path -Path $srcpath) {
Write-OutLog "Path is found; trying to verify subfolders under $srcpath"
if (!(Test-Path -Path "$srcpath\pipelines")) { Write-OutLog "Creating pipelines folder: $srcpath\pipelines"; new-item -path "$srcpath" -ItemType Directory }
if (!(Test-Path -Path "$srcpath\datasets")) { Write-OutLog "Creating datasets folder: $srcpath\datasets"; new-item -path "$srcpath" -ItemType Directory }
if (!(Test-Path -Path "$srcpath\linkedservices")) { Write-OutLog "Creating linkedservices folder: $srcpath\linkedservices "; new-item -path "$srcpath" -ItemType Directory }
if (!(Test-Path -Path "$srcpath\dataflows")) { Write-OutLog "Creating linkedservices folder: $srcpath\dataflows"; new-item -path "$srcpath" -ItemType Directory }
if (!(Test-Path -Path "$srcpath\integrationruntimes")) { Write-OutLog "Creating linkedservices folder: $srcpath\integrationruntimes"; new-item -path "$srcpath" -ItemType Directory }
if (!(Test-Path -Path "$srcpath\triggers")) { Write-OutLog "Creating linkedservices folder: $srcpath\triggers"; new-item -path "$srcpath" -ItemType Directory }
}
else {
Write-OutLog "Creating $srcpath from scratch...";
new-item -path "$srcpath" -ItemType Directory
new-item -path "$srcpath\pipelines" -ItemType Directory
new-item -path "$srcpath\datasets" -ItemType Directory
new-item -path "$srcpath\linkedservices" -ItemType Directory
new-item -path "$srcpath\dataflows" -ItemType Directory
new-item -path "$srcpath\integrationruntimes" -ItemType Directory
new-item -path "$srcpath\triggers" -ItemType Directory
}
}
function check-pipelinelastrun($adf, $rg, $pipeline, [int]$months = 8) {
$todayDateStamp = [DateTime]::UtcNow.ToString("yyyy-MM-ddTHH\:mm\:ss.fffffffZ") #az datafactory command expects ISO8601 timestamp format
$oldDateStamp = "2015-01-01T00:00:00.00Z" #predates ARM, should be safe as a baseline date range.
$runs = Invoke-AzCmd "az datafactory pipeline-run query-by-factory --resource-group $rg --factory-name $adf --last-updated-after $oldDateStamp --last-updated-before $todayDateStamp --filters operand=`"PipelineName`" operator=`"Equals`" values=`"$pipeline`""
Write-OutLog "Pipeline $pipeline has this many runs: $($runs.Value.Count)"
if ($runs.Value.Count -gt 0) {
$lastRun = [datetime]::parse(($runs.value | Sort-Object -Property runEnd -Descending)[0].runEnd)
}
else {
return $false
}
return ([DateTime]::Compare($lastRun, [DateTime]::Now.AddMonths($months * -1)) -gt 0)
}
function get-datasets($json) {
$type = $json.id.split("/")[9]
$datasets = @()
switch ($type) {
"pipelines" {
foreach ($activity in $json.properties.activities) {
Write-OutLog "checking activity $($activity.name) for pipeline $($json.name)"
foreach ($input in $activity.inputs) {
if ($input.type -eq "DatasetReference") {
Write-OutLog "found dataset $($input.referenceName) for pipeline $($json.name)"
$datasets += $input.referenceName
}
}
foreach ($output in $activity.outputs) {
if ($output.type -eq "DatasetReference") {
Write-OutLog "found dataset $($output.referenceName) for pipeline $($json.name)"
$datasets += $output.referenceName
}
}
}
}
"dataflows" {
foreach ($source in $json.properties.typeProperties.sources) {
if ($source.dataset.type -eq "DatasetReference") { $datasets += $source.dataset.referenceName }
}
foreach ($sink in $json.properties.typeProperties.sinks) {
if ($sink.dataset.type -eq "DatasetReference") { $datasets += $sink.dataset.referenceName }
}
}
}
return $datasets
}
function get-dataflows($json) {
$type = $json.id.split("/")[9]
$dataflows = @()
switch ($type) {
"pipelines" {
foreach ($activity in $json.properties.activities) {
foreach ($property in $activity.typeProperties) {
if ($property.dataflow -ne $null) {
$dataflows += $property.dataflow.referenceName
}
}
}
}
}
return $dataflows
}
function get-linkedservices($json) {
$type = $json.id.split("/")[9]
$linkedservices = @()
switch ($type) {
"pipelines" {
foreach ($activity in $json.properties.activities) {
foreach ($linkedservice in $activity.linkedServiceName) {
if ($linkedservice.type -eq "LinkedServiceReference") { $linkedservices += $linkedservice.referenceName }
}
}
}
"datasets" {
foreach ($linkedservice in $json.properties.linkedServiceName) {
if ($linkedservice.type -eq "LinkedServiceReference") { $linkedservices += $linkedservice.referenceName }
}
}
"dataflows" {
foreach ($source in $json.properties.typeProperties.sources) {
if ($source.linkedService.type -eq "LinkedServiceReference") { $linkedservices += $source.linkedService.referenceName }
}
foreach ($sink in $json.properties.typeProperties.sinks) {
if ($sink.linkedService.type -eq "LinkedServiceReference") { $linkedservices += $sink.linkedService.referenceName }
}
}
}
return $linkedservices
}
function get-dataflowdatasets($dataflows, $factory, $resourceGroup, $subscription) {
$datasets = @()
#get dataflow JSON
foreach ($dataflow in $dataflows) {
$dataflowuri = "`'https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory)/dataFlows/$($dataflow)?api-version=2018-06-01`'"
$dataflowobj = (Invoke-AzCmd "az rest --uri $dataflowuri --method get")
$datasets += (get-datasets $dataflowobj)
}
return $datasets
}
function backup-factories($sub, $resourceGroup, $srcfolder, $filter = $false, $lookbackMonths = 12) {
Write-OutLog "Backup factories running on sub: $sub with RG: $resourceGroup with source folder: $srcfolder"
$factories = (Invoke-AzCmd "az resource list --resource-group $resourceGroup --resource-type `"Microsoft.Datafactory/factories`"")
foreach ($factory in $factories) {
Write-OutLog "Starting backup of Factory $($factory.name) in resource group $resourceGroup"
#make sure all subfolders exist first...
ensure-adfdirectory -srcpath "$srcfolder\$($factory.name)"
#backup pipelines first
$uri = "https://management.azure.com/subscriptions/$sub/resourcegroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/pipelines?api-version=2018-06-01"
foreach ($pipeline in (Invoke-AzCmd "az rest --uri $uri --method get")) {
#Don't back up if not run in last X months...
if (($Filter -and (check-pipelinelastrun -adf $factory.name -rg $resourceGroup -pipeline $pipeline.name -months $lookbackMonths)) -or !($Filter)) {
Write-OutLog "Found pipeline: $($pipeline.name)" -ForegroundColor Green
backup-adfpipeline -sub $subscription -rg $resourceGroup -adf $factory.name -pipeline $pipeline.name -outputfile "$srcfolder\$($factory.name)\pipelines\$($pipeline.name).json"
}
}
#backup dataflows
$dataflowuri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/dataflows?api-version=2018-06-01"
foreach ($dataflow in (Invoke-AzCmd "az rest --uri $dataflowuri --method get")) {
Write-OutLog "Found data flow: $($dataflow.name)" -ForegroundColor Green
backup-adfdataflow -sub $subscription -rg $resourceGroup -adf $factory.name -dataflow $dataflow.name -outputfile "$srcfolder\$($factory.name)\dataflows\$($dataflow.name).json"
}
#backup datasets
$dataseturi = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/datasets?api-version=2018-06-01"
foreach ($dataset in (Invoke-AzCmd "az rest --uri $dataseturi --method get")) {
Write-OutLog "Found data set: $($dataset.name)" -ForegroundColor Green
backup-adfdataset -sub $subscription -rg $resourceGroup -adf $factory.name -dataset $dataset.name -outputfile "$srcfolder\$($factory.name)\datasets\$($dataset.name).json"
}
#backup linked services
$linkedservicesuri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/linkedservices?api-version=2018-06-01"
foreach ($service in (Invoke-AzCmd "az rest --uri $linkedservicesuri --method get")) {
Write-OutLog "Found linkedservice: $($service.name)" -ForegroundColor Green
backup-adflinkedservice -sub $subscription -rg $resourceGroup -adf $factory.name -linkedservice $service.name -outputfile "$srcfolder\$($factory.name)\linkedservices\$($service.name).json"
}
#backup integration runtimes
$integrationruntimeuri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/integrationruntimes?api-version=2018-06-01"
foreach ($runtime in (Invoke-AzCmd "az rest --uri $integrationruntimeuri --method get")) {
Write-OutLog "Found integration runtime: $($runtime.name)" -ForegroundColor Green
backup-adfintegrationruntime -sub $subscription -rg $resourceGroup -adf $factory.name -linkedservice $runtime.name -outputfile "$srcfolder\$($factory.name)\integrationruntimes\$($runtime.name).json"
}
#backup triggers
$triggeruri = "https://management.azure.com/subscriptions/$subscription/resourceGroups/$resourceGroup/providers/Microsoft.DataFactory/factories/$($factory.name)/triggers?api-version=2018-06-01"
foreach ($trigger in (Invoke-AzCmd "az rest --uri $triggeruri --method get")) {
Write-OutLog "Found integration runtime: $($trigger.name)" -ForegroundColor Green
backup-adftrigger -sub $subscription -rg $resourceGroup -adf $factory.name -linkedservice $trigger.name -outputfile "$srcfolder\$($factory.name)\triggers\$($runtime.name).json"
}
#backup the factory itself
backup-adffactory -sub $subscription -rg $resourceGroup -adf $factory.name -outputfile "$srcfolder\$($factory.name)\$($factory.name).json"
}
}
function restore-factories {
[CmdletBinding()]
param (
$sub,
$resourceGroup,
$srcfolder,
$suffix = "",
$region = ""
)
Write-OutLog "Restore factories running on sub: $sub with RG: $resourceGroup with source folder: $srcfolder"
$srcdir = get-item -Path $srcfolder
foreach ($factory in $srcdir.GetDirectories()) {
Write-OutLog "Starting restore of Factory $($factory.Name) in resource group $resourceGroup"
try {
#suffix is included here to ensure the ADF name is unique globally. Backup and restore won't work if you haven't deleted the source factory otherwise.
deploy-adffactory -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -inputfile "$($factory.FullName)\$($factory.Name).json" -region $region
}
catch {
Write-OutLog $_.Exception -ForegroundColor Red
continue
}
# New/Restore ADF
# Pause needed as it fails if created too soon after ADF is created
Write-OutLog "Pause for 60 seconds..."
Start-Sleep -Seconds 30
Write-OutLog "30 seconds left..."
Start-Sleep -Seconds 20
Write-OutLog "10 seconds left..."
Start-Sleep -Seconds 10
$factoryPrincipalId = (Invoke-AzCmd -cmd "az datafactory list" -deserialize $true | Where-Object { $_.Name -eq "$($factory.name)$suffix" }).identity.principalId
if ($null -eq $factoryPrincipalId) { Write-OutLog -msg "Unable to get ID to " }
#deploy integration runtimes
foreach ($runtime in $factory.GetDirectories("integrationruntimes").GetFiles("*.json", [System.IO.SearchOption]::AllDirectories)) {
# Old/Existing ADF
$scope = (Get-Content $runtime.FullName | ConvertFrom-Json -Depth 4).properties.typeproperties.linkedInfo.resourceId
Write-OutLog "Scope is $scope"
Write-OutLog "Factory Pricipal ID is $factoryPrincipalId"
Write-OutLog "Setting Role for Integrated Runtime"
if (($null -eq $scope) -or ($scope -eq "") ) { Write-OutLog "Unable to set Integrated Runtime permissions, skipping." }
else { Invoke-AzCmd -cmd "az role assignment create --role 'Contributor' --assignee $factoryPrincipalId --scope $scope" -deserialize $false }
deploy-adfintegrationruntime -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -integrationruntime $runtime.BaseName -inputfile $runtime.FullName
}
#Set Role Assignment to System Indentity
Set-ADFManagedIdentity -factoryName $factory.name -newFactoryName "$($factory.name)$suffix"
#deploy linked services
foreach ($service in $factory.GetDirectories("linkedservices").GetFiles("*.json", [System.IO.SearchOption]::AllDirectories)) {
deploy-adflinkedservice -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -linkedservice $service.BaseName -inputfile $service.FullName
}
Write-OutLog "Deploying backed up Data sets..."
foreach ($dataset in $factory.GetDirectories("datasets").GetFiles("*.json", [System.IO.SearchOption]::AllDirectories)) {
Write-OutLog "found dataset $dataset"
$folder = $dataset.Directory.FullName.Replace("$($factory.FullName)\datasets", "").Trim("\").Replace("\", "/")
if ($null -ne $folder) { Write-OutLog "Folder is $folder" }
deploy-adfdataset -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -dataset $dataset.BaseName -inputfile $dataset.FullName -folder $folder
}
Write-OutLog "Deploying backed up Data flows..."
foreach ($dataflow in $factory.GetDirectories("dataflows").GetFiles("*.json", [System.IO.SearchOption]::AllDirectories)) {
Write-OutLog "found dataflow $dataflow"
$folder = $dataflow.Directory.FullName.Replace("$($factory.FullName)\dataflows", "").Trim("\").Replace("\", "/")
if ($null -ne $folder) { Write-OutLog "Folder is $folder" }
deploy-adfdataflow -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -dataflow $dataflow.BaseName -inputfile $dataflow.FullName -folder $folder
}
#deploy pipelines
$splat = @{
SouceDirectory = $srcfolder
Subscription = $subscription
ResourceGroup = $resourceGroup
ADF = "$($factory.name)$suffix"
}
Deploy-AdfPipelineDependancy @splat
# deploy triggers last
foreach ($trigger in $factory.GetDirectories("triggers").GetFiles("*.json", [System.IO.SearchOption]::AllDirectories)) {
Deploy-AdfTrigger -sub $subscription -rg $resourceGroup -adf "$($factory.name)$suffix" -trigger $trigger.BaseName -inputfile $trigger.FullName
}
}
}
function Deploy-AdfPipelineDependancy {
[CmdletBinding()]
param (
$SouceDirectory,
$Subscription,
$ResourceGroup,
$ADF
)
$folder = Get-childitem -path $SouceDirectory 'pipelines' -Recurse -Directory
$list = Get-ChildItem *.json -Path $folder
$dependsOn = @()
#build hashtable
foreach ($path in $list) {
$json = Get-Content $path.FullName
$get = $json | ConvertFrom-Json
if ($get.properties.activities.typeProperties.activities.typeProperties.pipeline) {
#"Found Dependancy"
$dependsOn += [PSCustomObject][Ordered]@{
Name = $path.BaseName
DependsOn = $get.properties.activities.typeProperties.activities.typeProperties.pipeline.referenceName
Deployed = $null
}
}
else {
#"No Depends On"
$dependsOn += [PSCustomObject][Ordered]@{
Name = $path.BaseName
DependsOn = $null
Deployed = $null
}
}
}
#deploy
$deployCount = ($dependsOn | Where-Object { $null -eq $_.Deployed }).count
while ($deployCount -gt 0) {
foreach ($path in $list) {
Write-OutLog -msg "Attempting to deploy pipeline $($path.BaseName)"
$get = $dependson | Where-Object { $_.Name -eq $path.BaseName }
if ($get.Deployed -ieq "X") { Write-OutLog -msg "$($path.BaseName) Pipeline Deployed" -ForegroundColor Green }
elseif ($null -ne $get.DependsOn ) {
Write-OutLog -msg "$($path.BaseName) Has Dependancy $($get.DependsOn)"
$depend = $get.DependsOn
$getdepend = $dependson | Where-object { $_.Name -eq $depend }
if ($null -eq $getdepend.Deployed) {
Write-OutLog -msg "$($path.BaseName) dependancy, $($get.DependsOn), not deployed yet" -ForegroundColor Yellow
}
else {
Write-OutLog -msg "Dependancy $($get.DependsOn) deployed. Deploying pipeline $($path.BaseName)!" -ForegroundColor Cyan
try {
deploy-adfpipeline -sub $subscription -rg $resourceGroup -adf $adf -pipeline $path.BaseName -inputfile $path.FullName
$get.Deployed = "X"
}
catch {
$message = $_.Exception
Write-OutLog -msg "Failed to deploy pipeline $($path.BaseName)"
Write-OutLog $message -ForegroundColor Red
throw "Failed to deploy pipeline $($path.BaseName): $message"
}
}
}
else {
Write-OutLog -msg "$($path.BaseName) Does not have dependancy. Deploying pipeline." -ForegroundColor Green
try {
deploy-adfpipeline -sub $subscription -rg $resourceGroup -adf $adf -pipeline $path.BaseName -inputfile $path.FullName
$get.Deployed = "X"
}
catch {
$message = $_.Exception
Write-OutLog -msg "Failed to deploy pipeline $($path.BaseName)"
Write-OutLog $message -ForegroundColor Red
throw "Failed to deploy pipeline $($path.BaseName): $message"
}
}
}
$deployCount = ($dependsOn | Where-Object { $null -eq $_.Deployed }).count
}
}
function Set-ADFManagedIdentity {
[CmdletBinding()]
param (
$factoryName,
$newFactoryName
)
# figure out if it's system or user assigned, currently assuming system
#$type = (Get-Content (Get-ChildItem g-adf-2d-gdp-redaanalytics-01.json -Path $path).FullName | ConvertFrom-Json).identity.type
#if ($type -eq 'SystemAssigned') {
Write-OutLog "Setting System Managed Identity Roles..."
#Not using user assgined
##User Assgined Managed ID
#$managedIdName = (az identity list | ConvertFrom-Json | Where-Object{$_.principalId -eq $midPrincipalId}).name
#az ad sp list --display-name $managedIdName --query [].id --output tsv
#System Assigned Managed ID
# No output when using Invoke-AzCmd, need that output
$out = az ad sp list --display-name $factoryName | Convertfrom-Json
$oldaid = ($out | Where-Object { $_.displayName -eq $factoryName }).id
Write-OutLog "Assignee ID for old ADF is $oldaid"
#az role assignment list --assignee # uses graph API
# does not work returnns [] # only works if --all is added
$roles = az role assignment list --assignee $oldaid --all | ConvertFrom-Json -Depth 4
Write-OutLog "Role Information: "
Write-OutLog $roles
$aid = az ad sp list --display-name $newFactoryName --query [].id --output tsv
Write-OutLog "Assignee ID for new ADF is $aid"
# Assign Role
foreach ($role in $roles) {
Invoke-AzCmd -cmd "az role assignment create --assignee ""$aid"" --role ""$($role.roleDefinitionName)"" --scope ""$($role.scope)"" "
}
}